{"id":346,"date":"2022-11-23T19:26:06","date_gmt":"2022-11-23T19:26:06","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=346"},"modified":"2022-11-23T19:29:10","modified_gmt":"2022-11-23T19:29:10","slug":"java-working-with-generics","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/","title":{"rendered":"Java &#8211; Working with Generics"},"content":{"rendered":"<p>This would be a practical tutorial on working with generics in Java. It would be step by step simple explanation with examples<\/p>\n<p><strong>Content<\/strong><\/p>\n<ol>\n<li><a href=\"#t1\">Why Do We Need Generics<\/a><\/li>\n<li><a href=\"#t2\">How Generics Help in Code Reuse<\/a><\/li>\n<li><a>Bounded Generic Classes<\/a><\/li>\n<li><a href=\"#t4\">Generic Methods<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Why Do We Need Generics<\/strong><\/h4>\n<p>Assuming we need a class that contains a method that takes in an integer argument and prints it to the console. We would have something like shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">IntegerPrinter<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> Integer toPrint<span style=\"color: #333333;\">;<\/span>\r\n\t\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">IntegerPrinter<\/span><span style=\"color: #333333;\">(<\/span>Integer toPrint<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n       <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">toPrint<\/span> <span style=\"color: #333333;\">=<\/span> toPrint<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\t\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">print<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t \r\n        System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>toPrint<span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Then we can call this class from the main method like so<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n    IntegerPrinter printer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> IntegerPrinter<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">34<\/span><span style=\"color: #333333;\">);<\/span>\r\n    printer<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The problem with this is that when we want to print a double, float, string or any other type, this code would not do. We would need to create different classes for them. For instance: <em>DoublePrinter<\/em>, <em>FloatPrinter<\/em> etc. This lead to much code duplication<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. How Generics Solve the Problem<\/strong><\/h4>\n<p>We could simply modify the IntegerPrinter class to accept a generic type. The type would be specified\u00a0 when the class is called.<\/p>\n<p>The new class would look like this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Printer<\/span><span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #333333;\">{ \/\/ You can have more multiple generics as well<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> T toPrint<span style=\"color: #333333;\">;<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Printer<\/span><span style=\"color: #333333;\">(<\/span>T toPrint<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">toPrint<\/span> <span style=\"color: #333333;\">=<\/span> toPrint<span style=\"color: #333333;\">;<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">print<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>toPrint<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>What we did here was to use a generic argument, T which could be any type. So when the Printer is called, we then have to provide the type parameter. This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\tPrinter<span style=\"color: #333333;\">&lt;<\/span>Double<span style=\"color: #333333;\">&gt;<\/span> printer1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Printer<span style=\"color: #333333;\">&lt;&gt;(<\/span><span style=\"color: #6600ee; font-weight: bold;\">34.5<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tprinter1<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\r\n\tPrinter<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">&gt;<\/span> printer2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Printer<span style=\"color: #333333;\">&lt;&gt;(<\/span><span style=\"color: #0000dd; font-weight: bold;\">34<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tprinter2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\r\n\tPrinter<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> printer3 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Printer<span style=\"color: #333333;\">&lt;&gt;(<\/span><span style=\"background-color: #fff0f0;\">\"Hello World\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tprinter3<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So you can see here that we used the same class for printing Integer, Double and String.<\/p>\n<p><strong>Note<\/strong>: Generics don&#8217;t work with primitive types. You must use wrapper classes.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Bounded Generics<\/strong><\/h4>\n<p>In the previous section, considered a class with a generic type. So the class can be instantiated with any type. Assuming we want to limit the types that can be used. We could then use bounded generics. Here, we want the generic type to be only subclasses of a given class. For example,\u00a0 let&#8217;s say we have class Shape. And we have subclasses like: Circle, Triangle and Square.<\/p>\n<p>So we would like to generic type to be only one of these three. We would modify our generic type definition like this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Printer<\/span><span style=\"color: #333333;\">&lt;<\/span>T <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Shape<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So in the main method, we can now create a Printer object, but the only type argument would be those derived from Shape. This is shown below:<\/p>\n<p><strong>Note<\/strong>: I already create Shape, Triangle, Circle and Square. Get all codes here.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\tPrinter<span style=\"color: #333333;\">&lt;<\/span>Circle<span style=\"color: #333333;\">&gt;<\/span> printer1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Printer<span style=\"color: #333333;\">&lt;&gt;(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Circle<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">));<\/span>\r\n\tPrinter<span style=\"color: #333333;\">&lt;<\/span>Square<span style=\"color: #333333;\">&gt;<\/span> printer2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Printer<span style=\"color: #333333;\">&lt;&gt;(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Square<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">));<\/span>\r\n\t\r\n\tPrinter<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">&gt;<\/span> printer3 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Printer<span style=\"color: #333333;\">&lt;&gt;(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #888888;\">\/\/ Throws an exception<\/span>\r\n\t\r\n\tCircle c1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Circle<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tSquare s2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Square<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\r\n\t\t\t\r\n\tprinter1<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n\tprinter2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n\tc1<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">();<\/span>\r\n\ts2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printSides<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Note that we have called the printSides() method available in the Shape class. So we are able to use it. This is possible because the generic type extends the Shape class. You can also extend interfaces as well. This ensures that your the type would implement the interfaces. But you can have multiple bounds: you can have one class and multiple interfaces.<\/p>\n<p>Watch the video for explanation.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Generic Methods<\/strong><\/h4>\n<p>You can also create a method that takes in a generic type. Here, the type would be inferred from the arguments provided when the method is invoked. For example, the function below take two generic types: T and V. Then when we call it we provide a string and an integer and it prints them out<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Generics<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t\tshow<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Student\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">19<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tshow<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Employee\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">45<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">,<\/span> V<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> show<span style=\"color: #333333;\">(<\/span>T data1<span style=\"color: #333333;\">,<\/span> V data2<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"First argument: \"<\/span> <span style=\"color: #333333;\">+<\/span> data1<span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Second argument: \"<\/span> <span style=\"color: #333333;\">+<\/span> data2<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can see from the code that we did not provide any specific type for the parameters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This would be a practical tutorial on working with generics in Java. It would be step by step simple explanation with examples Content Why Do &hellip; <\/p>\n","protected":false},"author":395,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[24,23],"class_list":["post-346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-bounded-generics","tag-generics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Working with Generics - Java Tutorials<\/title>\n<meta name=\"description\" content=\"This is a tutorial on Working With Generics in Java. It&#039;s a step by step explanation with examples and code snippets\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java - Working with Generics - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"This is a tutorial on Working With Generics in Java. It&#039;s a step by step explanation with examples and code snippets\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-23T19:26:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-23T19:29:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2022\/11\/Working-With-Generics-in-Java-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1219\" \/>\n\t<meta property=\"og:image:height\" content=\"417\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Working with Generics\",\"datePublished\":\"2022-11-23T19:26:06+00:00\",\"dateModified\":\"2022-11-23T19:29:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/\"},\"wordCount\":518,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2022\\\/11\\\/Working-With-Generics-in-Java-1.jpg\",\"keywords\":[\"Bounded Generics\",\"Generics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/\",\"name\":\"Java - Working with Generics - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2022\\\/11\\\/Working-With-Generics-in-Java-1.jpg\",\"datePublished\":\"2022-11-23T19:26:06+00:00\",\"dateModified\":\"2022-11-23T19:29:10+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"This is a tutorial on Working With Generics in Java. It's a step by step explanation with examples and code snippets\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2022\\\/11\\\/Working-With-Generics-in-Java-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2022\\\/11\\\/Working-With-Generics-in-Java-1.jpg\",\"width\":1219,\"height\":417,\"caption\":\"Working With Generics in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/java-working-with-generics\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Working with Generics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Working with Generics - Java Tutorials","description":"This is a tutorial on Working With Generics in Java. It's a step by step explanation with examples and code snippets","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/","og_locale":"en_US","og_type":"article","og_title":"Java - Working with Generics - Java Tutorials","og_description":"This is a tutorial on Working With Generics in Java. It's a step by step explanation with examples and code snippets","og_url":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/","og_site_name":"Java Tutorials","article_published_time":"2022-11-23T19:26:06+00:00","article_modified_time":"2022-11-23T19:29:10+00:00","og_image":[{"width":1219,"height":417,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2022\/11\/Working-With-Generics-in-Java-1.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Working with Generics","datePublished":"2022-11-23T19:26:06+00:00","dateModified":"2022-11-23T19:29:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/"},"wordCount":518,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2022\/11\/Working-With-Generics-in-Java-1.jpg","keywords":["Bounded Generics","Generics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/","url":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/","name":"Java - Working with Generics - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2022\/11\/Working-With-Generics-in-Java-1.jpg","datePublished":"2022-11-23T19:26:06+00:00","dateModified":"2022-11-23T19:29:10+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"This is a tutorial on Working With Generics in Java. It's a step by step explanation with examples and code snippets","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2022\/11\/Working-With-Generics-in-Java-1.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2022\/11\/Working-With-Generics-in-Java-1.jpg","width":1219,"height":417,"caption":"Working With Generics in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/java-working-with-generics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Working with Generics"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=346"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/346\/revisions\/351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/352"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}