{"id":95,"date":"2013-07-02T11:03:33","date_gmt":"2013-07-02T11:03:33","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=95"},"modified":"2013-07-09T12:13:25","modified_gmt":"2013-07-09T12:13:25","slug":"generics-note1","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/generics-note1\/","title":{"rendered":"Generics Notes1"},"content":{"rendered":"<p><strong>Need for Generics<\/strong><br \/>\nPre Java 5 Containers allows to insert an incorrect type into a Container.Consider a container you need to store Apple objects in it.The code for scenario is as shown below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Generics1\r\n{\r\n  public static void main(String&#x5B;] args)\r\n  {\r\n    List arrFruits = new ArrayList();\r\n    arrFruits.add(new Apple());\r\n    arrFruits.add(new Mango());\r\n\t\t\r\n    for (Object object : arrFruits)\r\n    {\r\n     System.out.println(((Apple)object).Name);\r\n    }\r\n  }\r\n}\r\n\r\nclass Apple\r\n{\t\r\n  String Name = &quot;Ooty&quot;;\r\n}\r\n\r\nclass Mango\r\n{\t\r\n  String Name = &quot;Malgova&quot;;\r\n}\r\n<\/pre>\n<p>Now the above code compiles Perfectly fine.But when you try to run the code you will encounter a problem in casting to Apple class as you added Mango object in to arrFruits.The code compiles without error since every thing you add using add method into ArrayList gets converted to Object before it get Stored.  <\/p>\n<p>By Using generics you are preventing the addition of wrong type of Object into the container as Below.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Generics1\r\n{\t\r\n public static void main(String&#x5B;] args)\r\n {\r\n   List&lt;Apple&gt; arrFruits = new ArrayList&lt;Apple&gt;();\r\n   arrFruits.add(new Apple());\r\n   arrFruits.add(new Mango()); \/\/Compile Time Error\r\n\t\t\r\n   for (Apple object : arrFruits)\r\n   {\r\n     System.out.println(object.Name);\r\n   }\r\n  }\r\n}\r\n<\/pre>\n<p>You can also see there is no need for Casting while using Generics.<\/p>\n<p>You can also store Subtype of Parent class in Generics as Below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\npublic class Generics1\r\n{\t\r\n public static void main(String&#x5B;] args)\r\n {\r\n   ArrayList&lt;Fruits&gt; arrFruits = new ArrayList&lt;Fruits&gt;();\r\n   arrFruits.add(new Fruits());\r\n   arrFruits.add(new Apple());\r\n   arrFruits.add(new Orange());\r\n   arrFruits.add(new Mango());\r\n\t\r\n   for (Fruits object : arrFruits)\r\n   {\r\n     System.out.println(object.Name);\r\n   }\r\n }\r\n}\r\n\r\nclass Fruits\r\n{\t\r\n  String Name = &quot;I am Fruit&quot;;\r\n}\r\n\r\nclass Apple extends Fruits\r\n{\t\r\n  public Apple()\r\n  {\r\n    Name = &quot;I am Apple&quot;;\r\n  }\r\n}\r\n\r\nclass Orange extends Fruits\r\n{\t\r\n  public Orange()\r\n  {\r\n    Name = &quot;I am Orange&quot;;\r\n  }\r\n}\r\n\r\nclass Mango extends Fruits\r\n{\t\r\n   public Mango()\r\n   {\r\n     Name = &quot;I am Mango&quot;;\r\n   }\r\n}\r\n<\/pre>\n<p>The Containers can be Broadly Classified in to Two types.<\/p>\n<p>1.Collection<br \/>\n2.Map<\/p>\n<p>The Collection includes the following<br \/>\n1.ArrayList<br \/>\n2.LinkedList<br \/>\n3.HashSet<br \/>\n4.TreeSet<br \/>\n5.LinkedHashSet<\/p>\n<p>The Map includes the following<br \/>\n1.HashMap<br \/>\n2.TreeMap<br \/>\n3.LinkedHashMap<\/p>\n<p><strong>Boxing<\/strong><br \/>\nBoxing is the process of converting the Primitive type to Reference type<\/p>\n<blockquote><p>byte to Byte<br \/>\nint to Integer<br \/>\ndouble to Double<br \/>\nchar to Character<\/p><\/blockquote>\n<p><strong>Unboxing<\/strong><br \/>\nUnboxing is the process of converting the Reference type to Primitive type<\/p>\n<blockquote><p>Byte to byte<br \/>\nInteger to int<br \/>\nDouble to double<\/p><\/blockquote>\n<p>Consider the Following example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList arrAges = new ArrayList();\r\narrAges.add(25);\r\nint n = arrAges.get(0);\r\n<\/pre>\n<p>is equivalent to<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList arrAges = new ArrayList();\r\narrAges.add(new Integer(25));\r\nint n = arrAges.get(0).intValue();\r\n<\/pre>\n<p><strong>Boxing an int or short value between -128 and 127, a Char value between &#8216;\\u0000&#8217; and &#8216;\\u007f&#8217;, byte and boolean<\/strong><\/p>\n<p>Consider the following example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nInteger a = 3;\r\nInteger b = 2;\r\nInteger c = 5;\r\nInteger d = a + b;\r\nSystem.out.println(c == d);\r\n<\/pre>\n<p><strong>Output<\/strong><br \/>\n<strong>true<\/strong><br \/>\nThe output is true because the value comparison is taking place instead of object comparison.Boxed values between -128 to 127 are cached. Boxing uses Integer.valueOf method, which uses the cache. Values outside the range are not cached and always created as a new instance. Since your values fall into the cached range, values are equal using == operator.<\/p>\n<p>This is What happening when values boxed are between -128 to 127<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nInteger a = 3;\r\nInteger b = 2;\r\nInteger c = 5;\r\nInteger c = Integer.valueOf(5);\r\nInteger d = Integer.valueOf(a.intValue() + b.intValue());\r\n<\/pre>\n<p><strong>Output<\/strong><br \/>\ntrue<\/p>\n<p>when you try to add integers which are outside the range as one below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nInteger a = 300;\r\nInteger b = 200;\r\nInteger c = 500;\r\nInteger d = a + b;\r\nSystem.out.println(c == d);\r\n<\/pre>\n<p><strong>Output<\/strong><br \/>\nfalse<\/p>\n<p><strong>How to Check Content in List with String<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List arrNums  = new ArrayList();\r\n arrNums.add(2);\r\n arrNums.add(3.14);\r\n assert arrNums.toString().equals(&quot;&#x5B;2,3.14]&quot;);\r\n<\/pre>\n<p>List is not a Subtype of List<br \/>\nList is a Subtype of Collection<\/p>\n<p><strong>Doesn&#8217;t Works<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List ints = Arrays.asList(1,2);\r\n List nums = ints;  \/\/Not Ok as you try to Assign List to List\r\n<\/pre>\n<p>The above doesn&#8217;t works as you try to add List to List<\/p>\n<p><strong>Works<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List ints1 = Arrays.asList(1,2);\r\n Collection nums = ints1;\r\n<\/pre>\n<p>Addition of Subtypes in Supertype in generics is Allowed as Below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList nums = new ArrayList();\r\nList ints2 = Arrays.asList(1,2);\r\nList ints3 = Arrays.asList(3.1,2.15);\r\n\r\nnums.add(3.15);\r\nnums.addAll(ints2);\r\nnums.addAll(ints3);\r\n<\/pre>\n<p>Assignment of Subtype to Supertype is not allowed<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List nums = new ArrayList();\r\n List ints = Arrays.asList(1,2);\r\n nums.addAll(ints); \/\/Allowed\r\n nums = ints; \/\/Not Allowed\r\n<\/pre>\n<p>Assignment of List to List is Not Allowed<\/p>\n<p>Use extends wildcard when you get values out of Structure and use super wildcard when you put values into structure<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List&lt;? extends Integer&gt; arrNums = new ArrayList();\r\n List arrInts           = new ArrayList();\r\n arrInts.add(1);\r\n arrInts.add(2);\r\n arrInts.add(3);\r\n\r\n arrNums.addAll(arrInts); \/\/Not Allowed since wildcard is extends(get)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List&lt;? super Integer&gt; arrNums = new ArrayList();\r\n List arrInts         = new ArrayList();\r\n arrInts.add(1);\r\n arrInts.add(2);\r\n arrInts.add(3);\r\n\r\n arrNums.addAll(arrInts); \/\/Allowed since wildcard is super(put)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Need for Generics Pre Java 5 Containers allows to insert an incorrect type into a Container.Consider a container you need to store Apple objects in it.The code for scenario is as shown below public class Generics1 { public static void main(String&#x5B;] args) { List arrFruits = new ArrayList(); arrFruits.add(new Apple()); arrFruits.add(new Mango()); for (Object object&hellip; <a href=\"https:\/\/codethataint.com\/blog\/generics-note1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-generics-and-collections"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":22,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}