{"id":3399,"date":"2019-05-12T18:50:31","date_gmt":"2019-05-12T18:50:31","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3399"},"modified":"2019-05-12T19:04:00","modified_gmt":"2019-05-12T19:04:00","slug":"misc-questions-in-collections","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/misc-questions-in-collections\/","title":{"rendered":"Misc Questions in Collections"},"content":{"rendered":"<p><strong>1.Why we are unable to add primitives as generic type<\/strong><br \/>\n<strong>Allowed<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List&lt;Integer&gt; arrAges = new ArrayList&lt;Integer&gt;();\r\n<\/pre>\n<p><strong>Not allowed<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List&lt;int&gt; arrAges = new ArrayList&lt;int&gt;();\r\n<\/pre>\n<p>This is to maintain <strong>backwards compatibility with previous JVM runtimes<\/strong> in the sense it could be referred by parent class instance Object. Generics in Java are encountered at compile time The compiler converts from generic type to right type as shown in the example below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList&lt;ClassA&gt; list = new ArrayList&lt;ClassA&gt;();\r\nlist.add(new ClassA());\r\nClassA a = list.get(0);\r\n<\/pre>\n<p>will be turned in to<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList list = new ArrayList();\r\nlist.Add(new ClassA());\r\nClassA a = (ClassA)list.get(0);\r\n<\/pre>\n<p>So anything that is used as generics has to be convert able to Object (in this example get(0) returns an Object), and the primitive types aren&#8217;t. So they can&#8217;t be used in generics.<\/p>\n<p><strong>2.How to have a Ordered Collections<\/strong><\/p>\n<ol>\n<li>keep the insertion order: LinkedHashSet and CopyOnWriteArraySet (thread-safe)<\/li>\n<li>keep the items sorted within the set: TreeSet, EnumSet (specific to enums) and ConcurrentSkipListSet (thread-safe)<\/li>\n<li><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1.Why we are unable to add primitives as generic type Allowed List&lt;Integer&gt; arrAges = new ArrayList&lt;Integer&gt;(); Not allowed List&lt;int&gt; arrAges = new ArrayList&lt;int&gt;(); This is to maintain backwards compatibility with previous JVM runtimes in the sense it could be referred by parent class instance Object. Generics in Java are encountered at compile time The compiler&hellip; <a href=\"https:\/\/codethataint.com\/blog\/misc-questions-in-collections\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-3399","post","type-post","status-publish","format-standard","hentry","category-generics-and-collections"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3399","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=3399"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3399\/revisions"}],"predecessor-version":[{"id":3400,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3399\/revisions\/3400"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}