{"id":13198,"date":"2015-08-05T10:00:10","date_gmt":"2015-08-05T10:00:10","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=13198"},"modified":"2020-09-01T07:08:23","modified_gmt":"2020-09-01T07:08:23","slug":"java-collections-framework","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/java-collections-framework\/","title":{"rendered":"Java Collections Framework"},"content":{"rendered":"<p>The Java Collections framework has reduced the cumbersome work of actually creating data structures like List, Set, Map. The basic idea behind Collections is that they are used to add, delete, search, sort and iterate over objects. This post will seek to explain the Java Collections framework.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ea7e02;color:#ea7e02\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ea7e02;color:#ea7e02\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.whizlabs.com\/blog\/java-collections-framework\/#Is_it_%E2%80%9CCollection%E2%80%9D_or_%E2%80%9CCollections%E2%80%9D\" >Is it \u201cCollection\u201d or \u201cCollections\u201d?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.whizlabs.com\/blog\/java-collections-framework\/#Vector_class\" >Vector class:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.whizlabs.com\/blog\/java-collections-framework\/#LinkedList_class\" >LinkedList class:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.whizlabs.com\/blog\/java-collections-framework\/#ArrayList_class\" >ArrayList class:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.whizlabs.com\/blog\/java-collections-framework\/#Generics_and_Collections\" >Generics and Collections:<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Is_it_%E2%80%9CCollection%E2%80%9D_or_%E2%80%9CCollections%E2%80%9D\"><\/span>Is it \u201cCollection\u201d or \u201cCollections\u201d?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>At the outset, the word \u201cCollection\u201d in Java might be misunderstood for other terms. It is always important to note if it is \u2018Collection\u2019 or \u2018Collections\u2019. We have \u2018java.util.Collection\u2019 which is an interface and which is our primary point of discussion in this post. We also have\u2019java.util.Collections\u2019 which is a class with most static methods such as addAll, checkedListand so on.\u00a0\u00a0 According to the Oracle website, the Java Collections framework consists of interfaces, its implementations and algorithms.<\/p>\n<p>Some of the different sub- interfaces under the Collection interface are Set, List, Queue. Some of the important implementing classes of List are LinkedList, ArrayList, and Vector. Let us see the Vector class and LinkedList class before moving onto the ArrayList.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Vector_class\"><\/span>Vector class:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>For those of us working with Java from its earlier days in the late 90s, the Vector class is a familiar name. The Vector class is similar to an array and can hold elements and grow as needed. It is synchronized and has been transformed to the new \u2018ArrayList\u2019 class. If a thread safe environment is not needed, the ArrayList class is recommended to be used.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"LinkedList_class\"><\/span>LinkedList class:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>LinkedList is again another fundamental data structure that has been implemented in Java collections. Since the linked list is a doubly linked list rather than a single linked one, there are more methods for adding elements towards the end or the beginning. Adding or deleting elements to a doubly linked list is easier than iterating over it.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ArrayList_class\"><\/span>ArrayList class:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>As already stated, the ArrayList is also an array but which can grow on demand. It is unsynchronized and perfect for situations when the performance overhead of thread safety is unnecessary.<\/p>\n<p>The new ArrayList class is much more powerful for Collection operations like insertion and searching. An ArrayList is used to store objects of different type and can be used with Generics. The following program is a simple illustration of the ArrayList class:<\/p>\n<pre><strong>package<\/strong> collection;\n<strong>import<\/strong> java.util.*;\n<strong>public<\/strong> <strong>class<\/strong> col_1 {\n\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String[] args) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ <strong>TODO<\/strong> Auto-generated method stub\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 List&lt;Integer&gt; a1=<strong>new<\/strong> ArrayList&lt;Integer&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a1.add(2);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a1.add(45);\n\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(Integer a2:a1){\n\u00a0\u00a0\u00a0 \u00a0System.<strong><em>out<\/em><\/strong>.println(a2);\n\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0 a1.set(1, 25);\/\/ The number at 1st position is set to '25'\n\u00a0\u00a0\u00a0\u00a0 a1.add(8);\n\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"The list now is\" );\n\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(Integer a2:a1){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.<strong><em>out<\/em><\/strong>.println(a2);\n\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0 Collections.<em>sort<\/em>(a1);\n\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"The list after sorting is\" );\n\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(Integer a2:a1){\n\u00a0\u00a0 \u00a0 \u00a0System.<strong><em>out<\/em><\/strong>.println(a2);\n\u00a0\u00a0\u00a0\u00a0 }\n}\n}<\/pre>\n<p>The above program is initially used to create an ArrayList of Integer objects. Two numbers are added to it, which are printed out.\u00a0 The number at the 1<sup>st<\/sup> position is modified and a 3<sup>rd<\/sup> number is added to it thereby emphasizing the dynamic growth of the ArrayList.<\/p>\n<p>Once there are three numbers in the list, they are sorted according to the natural order by specifying the method from the java.util.Collections class<\/p>\n<p>Collections.sort(a1);<\/p>\n<p>The output of the program is as follows:<\/p>\n<p>2<br \/>\n45<\/p>\n<p>The list now is<\/p>\n<p>2<br \/>\n25<br \/>\n8<\/p>\n<p>The list after sorting is<\/p>\n<p>2<br \/>\n8<br \/>\n25<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Generics_and_Collections\"><\/span>Generics and Collections:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>J2SE 5 introduced \u2018Generics\u2019 which enabled Collections to store objects of similar type. This reduced the effort and errors caused by type casting.<\/p>\n<p>Prior to Java 5, an ArrayList had to be created as follows:<\/p>\n<pre>List a1=<strong>new<\/strong> ArrayList();\n\u00a0 a1.add(2);\n<strong>\u00a0 int<\/strong> i=(<strong>int<\/strong>)a1.get(0); \/\/ type casting needed to retrieve elements\n\u00a0System.<strong><em>out<\/em><\/strong>.println(i);<\/pre>\n<p>By means of Generics, the ArrayList class can be created as follows:<\/p>\n<pre>List&lt;String&gt; a =<strong>new<\/strong> ArrayList&lt;String&gt;();\n\u00a0\u00a0\u00a0\u00a0 a.add(\"abc\");\nSystem.<strong><em>out<\/em><\/strong>.println(a.get(0)); \/\/ type casting not needed<\/pre>\n<p>Generics makes use of \u00a0\u201c&lt;&gt;\u201d brackets to specify what type of objects an ArrayList can store. In the above example, the ArrayList is used to store \u2018String\u2019 objects.<\/p>\n<p>We have seen Java Collections and some important classes relating to the List interface in this post. We will explore more of Collections in subsequent posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Java Collections framework has reduced the cumbersome work of actually creating data structures like List, Set, Map. The basic idea behind Collections is that they are used to add, delete, search, sort and iterate over objects. This post will seek to explain the Java Collections framework. Is it \u201cCollection\u201d or \u201cCollections\u201d? At the outset, the word \u201cCollection\u201d in Java might be misunderstood for other terms. It is always important to note if it is \u2018Collection\u2019 or \u2018Collections\u2019. We have \u2018java.util.Collection\u2019 which is an interface and which is our primary point of discussion in this post. We also have\u2019java.util.Collections\u2019 which [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[975],"class_list":["post-13198","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-collections-framework"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":0,"uagb_excerpt":"The Java Collections framework has reduced the cumbersome work of actually creating data structures like List, Set, Map. The basic idea behind Collections is that they are used to add, delete, search, sort and iterate over objects. This post will seek to explain the Java Collections framework. Is it \u201cCollection\u201d or \u201cCollections\u201d? At the outset,&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/13198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=13198"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/13198\/revisions"}],"predecessor-version":[{"id":75989,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/13198\/revisions\/75989"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=13198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=13198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=13198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}