{"id":2024,"date":"2011-04-15T12:32:10","date_gmt":"2011-04-15T07:02:10","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2024"},"modified":"2011-04-15T12:32:10","modified_gmt":"2011-04-15T07:02:10","slug":"complete-java-collection-tutorial-for-the-beginner","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/complete-java-collection-tutorial-for-the-beginner\/","title":{"rendered":"Complete Java Collection tutorial for the beginner"},"content":{"rendered":"<p>Collection represents the group of objects. Depending on the method of storing and retrieving, collections are basically divided into three parts \u2013 <strong><a title=\"Set API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/Set.html\">Set<\/a>, <a title=\"Map API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/Map.html\">Map <\/a>and <a title=\"List API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/List.html\">List<\/a><\/strong>. Where Set does <strong>not contain duplicate <\/strong>values, Map contains <strong>key value <\/strong>type of data whereas List can have a duplicate values stored in it <strong>sequentially<\/strong>. This framework is provided in &#8220;<em>java.util<\/em>&#8221; package. <a title=\"Collection API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/Collection.html\">Collection <\/a>is the parent interface of all collections in java.<\/p>\n<p>Following list describes the core collection interfaces.<\/p>\n<p style=\"text-align: center;\">&nbsp;<\/p>\n<figure id=\"attachment_2025\" aria-describedby=\"caption-attachment-2025\" style=\"width: 440px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Collection-Interfaces.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2025 \" title=\"Java Collection Interfaces\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Collection-Interfaces.jpg?resize=440%2C151&#038;ssl=1\" alt=\"Java Collection Interfaces\" width=\"440\" height=\"151\" \/><\/a><figcaption id=\"caption-attachment-2025\" class=\"wp-caption-text\">Java Collection Interfaces<\/figcaption><\/figure>\n<p><strong><!--more-->Collection<\/strong> &#8220;\u201d the<strong> root of the collection <\/strong>hierarchy. A collection represents a group of objects known as its elements. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn&#8217;t provide any direct implementations of this interface but provides implementations of more specific sub interfaces, such as Set and List.<\/p>\n<p><strong>Set<\/strong> &#8220;\u201d a collection that cannot contain duplicate elements<\/p>\n<p><strong>List<\/strong> &#8220;\u201d an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).<\/p>\n<p><strong>Queue<\/strong> &#8220;\u201d a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.<br \/>\nQueues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements&#8217; natural ordering.<\/p>\n<p><strong>Map<\/strong> &#8220;\u201d an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you&#8217;ve used Hashtable, you&#8217;re already familiar with the basics of Map.<\/p>\n<p style=\"text-align: center;\">&nbsp;<\/p>\n<hr \/>\n<p><strong>Concrete Classes :<\/strong><\/p>\n<figure id=\"attachment_2026\" aria-describedby=\"caption-attachment-2026\" style=\"width: 452px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Collection-interfaces-and-concrete-classes.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2026  \" title=\"Java Collection interfaces and concrete classes\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Collection-interfaces-and-concrete-classes.jpg?resize=452%2C344&#038;ssl=1\" alt=\"Java Collection interfaces and concrete classes\" width=\"452\" height=\"344\" \/><\/a><figcaption id=\"caption-attachment-2026\" class=\"wp-caption-text\">Java Collection interfaces and concrete classes<\/figcaption><\/figure>\n<p><strong><a title=\"HashSet API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/HashSet.html\">HashSet <\/a>:<\/strong><br \/>\nThis class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes <span style=\"text-decoration: underline;\">no guarantees as to the iteration order <\/span>of the set; in particular, it does not guarantee that the order will remain constant over time. This class <em>permits the null <\/em>element.<\/p>\n<p><a title=\"LinkedHashSet API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/LinkedHashSet.html\"><strong>LinkedHashSet :<\/strong><\/a><br \/>\nHash table and linked list implementation of the Set interface,<span style=\"text-decoration: underline;\"> with predictable iteration order<\/span>. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation).<\/p>\n<p><strong>Interface <a title=\"Interface SortedSet API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/SortedSet.html\">SortedSet<\/a>:<\/strong><br \/>\nA set that further guarantees that its iterator <span style=\"text-decoration: underline;\">will traverse the set in ascending element order<\/span>, sorted according to the natural ordering of its elements (see Comparable), or by a <a title=\"Comparator API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/Comparator.html\">Comparator <\/a>provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap).<\/p>\n<p><a title=\"TreeSet API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/TreeSet.html\"><strong>TreeSet:<\/strong><\/a><br \/>\nThis class implements the Set interface, backed by a TreeMap instance. This class <em><span style=\"text-decoration: underline;\">guarantees that the sorted set will be in ascending element order, <\/span><\/em>sorted according to the natural order of the elements (seeComparable), or by the comparator provided at set creation time, depending on which constructor is used.<\/p>\n<hr \/>\n<p><a title=\"ArrayList API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/ArrayList.html\"><strong>ArrayList:<\/strong><\/a><br \/>\nResizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.<\/p>\n<p><a title=\"Vector API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/Vector.html\"><strong>Vector:<\/strong><\/a><br \/>\nThis class is roughly equivalent to ArrayList except it is Synchronized.<\/p>\n<p><a title=\"LinkedList API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/LinkedList.html\"><strong>LinkedList:<\/strong><\/a><br \/>\nLinked list implementation of the List and Queue interfaces. Implements all optional operations, and permits all elements (including null).<\/p>\n<hr \/>\n<figure id=\"attachment_2029\" aria-describedby=\"caption-attachment-2029\" style=\"width: 502px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/JAVA-Map-interface-and-concrete-classes.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2029 \" title=\"JAVA Map interface and concrete classes\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/JAVA-Map-interface-and-concrete-classes.jpg?resize=502%2C251&#038;ssl=1\" alt=\"JAVA Map interface and concrete classes\" width=\"502\" height=\"251\" \/><\/a><figcaption id=\"caption-attachment-2029\" class=\"wp-caption-text\">JAVA Map interface and concrete classes<\/figcaption><\/figure>\n<p><a title=\"HashMap API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/HashMap.html\"><strong>HashMap:<\/strong><\/a><br \/>\nThe HashMap class is roughly equivalent toHashtable, except that it is <span style=\"text-decoration: underline;\"><em>unsynchronized and permits nulls<\/em><\/span>.<\/p>\n<p><a title=\"HashTable API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/Hashtable.html\"><strong>HashTable:<\/strong><\/a><br \/>\nHash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. This class makes <em>no guarantees as to the order of the map<\/em>; in particular, it does not guarantee that the order will remain constant over time.<\/p>\n<p><a title=\"LinkedHashMap API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/LinkedHashMap.html\"><strong>LinkedHashMap:<\/strong><\/a><br \/>\nHash table and linked list implementation of the Map interface,<span style=\"text-decoration: underline;\"><em> with predictable iteration order<\/em><\/span>. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.<\/p>\n<p><a title=\"TreeMap API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/TreeMap.html\"><strong>TreeMap:<\/strong><\/a><br \/>\nThis class guarantees that the map will be in <em><span style=\"text-decoration: underline;\">ascending key order, sorted according<\/span><\/em> to the natural order for the key&#8217;s class (seeComparable), or by the comparator provided at creation time, depending on which constructor is used.<\/p>\n<hr \/>\n<p><strong>Note :<\/strong><br \/>\nTo synchronize the Set Concrete classes, use below code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nSet s = Collections.synchronizedSet(CONCREATECLASS_OBJECT);\n<\/pre>\n<p>To Synchronize the List Concreate classes :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nList list = Collections.synchronizedList(CONCREATECLASS_OBJECT);\n<\/pre>\n<hr \/>\n<figure id=\"attachment_2030\" aria-describedby=\"caption-attachment-2030\" style=\"width: 439px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Collection-Summary-Chart.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2030 \" title=\"Collection Summary Chart\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Collection-Summary-Chart.jpg?resize=439%2C89&#038;ssl=1\" alt=\"Collection Summary Chart\" width=\"439\" height=\"89\" \/><\/a><figcaption id=\"caption-attachment-2030\" class=\"wp-caption-text\">Collection Summary Chart<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Complete JAVA Collection tutorial for the beginner<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[329],"class_list":["post-2024","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1943,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/what-is-the-need-to-override-hashcode-and-equals-method\/","url_meta":{"origin":2024,"position":0},"title":"What is the need to Override Hashcode() and equals() method","author":"Jitendra","date":"April 11, 2011","format":false,"excerpt":"Although there are lots of material are available on internet and API document about the necessity of the overriding the hashcode() and equals() method in Java but lots of new developers still not able to understand the necessity of hashcode() method. In this article, I will try to explain step\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"How Hashcode works in java","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/How-Hashcode-works-in-java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2195,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/step-by-step-tutorial-for-creation-guide-of-business-service-data-area-ui-map-and-bpa-script-in-ormb-ccb-oracle-utilities-for-beginners\/","url_meta":{"origin":2024,"position":1},"title":"Step by Step tutorial for creation of Business Service, Data Area, UI Map and BPA Script in ORMB \/ CC&#038;B \/ Oracle Utilities for beginners","author":"Jitendra","date":"June 14, 2011","format":false,"excerpt":"Step by Step tutorial for creation guide of Business Service, Data Area, UI Map and BPA Script in ORMB \/ CC&B \/ Oracle Utilities for beginners","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"UI Map in ORMB\/CC&B\/Oracle Utility with BPA Script","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/06\/UI-Map-in-ORMB-with-BPA-Script.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-destructor-finalize\/","url_meta":{"origin":2024,"position":2},"title":"Java Destructor &#8211; finalize()","author":"Jitendra","date":"July 1, 2010","format":false,"excerpt":"Concept of destructor in JAVA. finalize() method.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Java Destructor finalize()","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Trash.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3278,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/visualforce\/using-fieldset-with-visualforce-and-apex\/","url_meta":{"origin":2024,"position":3},"title":"Using FieldSet with Visualforce and Apex","author":"Jitendra","date":"May 3, 2013","format":false,"excerpt":"One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its \"Maintenance\"\u009d, if New Filed is Added or needed to remove field we have to modify our code every time. However, Thanks to Salesforce that we have \"Field Set\"\u009d. With the\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Create Field Set in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-immutable-class-in-java\/","url_meta":{"origin":2024,"position":4},"title":"How to create Immutable Class in Java","author":"Jitendra","date":"February 3, 2012","format":false,"excerpt":"creating the Immutable class in Java with simple source code example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1655,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/introduction-to-annotation-in-java\/","url_meta":{"origin":2024,"position":5},"title":"Introduction to Annotation in JAVA","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Introduction to Annotation in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2024"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2024\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}