{"id":3005,"date":"2020-04-30T18:54:38","date_gmt":"2020-04-30T13:24:38","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3005"},"modified":"2022-07-21T15:46:30","modified_gmt":"2022-07-21T10:16:30","slug":"collection-hierarchy","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/collection-hierarchy\/","title":{"rendered":"Collection Hierarchy"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">Java Collection framework<\/a> consists of Interfaces and Classes. It works with different types of collections such as List, Set, and Tree, etc.<\/span><\/p>\n<p><em><span style=\"font-weight: 400;\">The below diagram shows the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_collections_framework\" rel=\"nofollow noopener\" target=\"_blank\">collection framework hierarchy<\/a>.<\/span><\/em><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-3006 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_71-1.png\" alt=\"\" width=\"596\" height=\"486\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_71-1.png 750w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_71-1-300x245.png 300w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/p>\n<p><img decoding=\"async\" class=\"wp-image-3008 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_72-1.png\" alt=\"\" width=\"366\" height=\"297\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_72-1.png 438w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_72-1-300x243.png 300w\" sizes=\"(max-width: 366px) 100vw, 366px\" \/><\/p>\n<h2><strong>Collection framework mainly consists of four interfaces:<\/strong><\/h2>\n<p><b>1] List: <\/b><span style=\"font-weight: 400;\">It contains elements arranged in sequential order, and these elements can be accessed by using their index position (starting from 0). It implements ArrayList, Vector, and VectorList. Elements can be inserted in the list at any position but can be retrieved in the same order in which they are added. It can also store duplicate elements.<\/span><\/p>\n<p><b>2] Queue:<\/b><span style=\"font-weight: 400;\"> Queue contains similar elements in which new elements are added from one end, whereas removed from the other end (FIFO \u2013 First In First Out). It implements LinkedList and PriorityQueue.<\/span><\/p>\n<p><b>3] Set: <\/b><span style=\"font-weight: 400;\">Set is used to store the collection of <a href=\"https:\/\/www.h2kinfosys.com\/blog\/control-statements-in-java\/\">unique elements<\/a>. Duplicacy is not allowed in the Set. It does not store the elements in sequential order. While accessing the elements, we may not get the elements in the same order in which they are stored. HashSet, LinkedHashSet, and TreeSet implements the Set interface. Elements in the Set can only be iterated using Iterator.<\/span><\/p>\n<p><b>4] Map: <\/b><span style=\"font-weight: 400;\">Map stores the elements in the form of Key\/Value pairs. It is extended by SortedMap and implemented by HashMap and HashTable. It does not contain duplicate values, and each key can, at most, store one value.<\/span><\/p>\n<p><iframe title=\"What Is Collection In Java | Collections Frame Work In Java | Java Online Training | H2K Infosys\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/kZPbDrqh_wk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2><b>Methods of Collection Interface<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">The <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-collections\/\">collection interface<\/a> contains mainly 15 methods:<\/span><\/p>\n<p><b>1] add():<\/b><span style=\"font-weight: 400;\">\u00a0It is used to add or insert an element in the collection. If the element is successfully added to the collection, it will return true; otherwise, it will return false. If the element is already available in the collection, it will not allow duplicate entries.\u00a0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>2]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>addAll():<\/b><span style=\"font-weight: 400;\">\u00a0It adds a collection of elements to the existing collection. It will return true if the elements are already added; otherwise, it will return false.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>3]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>clear():<\/b><span style=\"font-weight: 400;\">\u00a0It will clear or remove all the elements from the collection.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>4]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>contains():<\/b><span style=\"font-weight: 400;\">\u00a0It will check if the element is present in the collection or not. It is also used to search for an element. Contains method will return true if the element is present in the collection; otherwise, it will return false.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>5]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>containsAll():<\/b><span style=\"font-weight: 400;\">\u00a0It will check if the specified collection of elements is present in the existing collection or not. It will return true if the calling collection contains all specified elements; otherwise, it will return false.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>6]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>equals():<\/b><span style=\"font-weight: 400;\">\u00a0It will check for equality with another object.\u00a0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>7]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>hashcode():<\/b><span style=\"font-weight: 400;\">\u00a0It will return the hash code number for the collection, and its return type is an integer.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>8]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>isEmpty():<\/b><span style=\"font-weight: 400;\">\u00a0It will return true if a collection is empty otherwise false.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>9]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>iterator():<\/b><span style=\"font-weight: 400;\">\u00a0It will return an iterator.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>10]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>remove():<\/b><span style=\"font-weight: 400;\">\u00a0It will remove a specified element from the collection.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>11]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>removeAll():<\/b><span style=\"font-weight: 400;\">\u00a0It will remove all elements from the collection.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>12]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>retainAll():<\/b><span style=\"font-weight: 400;\">\u00a0It will remove all the elements from the collection except the specified collection.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>13]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>size():<\/b><span style=\"font-weight: 400;\">\u00a0It will return the total number of elements present in the collection, and its return type is an integer.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>14]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>toArray():<\/b><span style=\"font-weight: 400;\">\u00a0It will return the elements present in a collection in the form of an array. The array elements are available as copies of the collection elements.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>15]<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>Object[ ] toArray():<\/b><span style=\"font-weight: 400;\">\u00a0It will return an array which will contain all the elements stored in the invoking collection. The array elements are available as copies of the collection elements.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Collection framework consists of Interfaces and Classes. It works with different types of collections such as List, Set, and Tree, etc. The below diagram shows the collection framework hierarchy. Collection framework mainly consists of four interfaces: 1] List: It contains elements arranged in sequential order, and these elements can be accessed by using their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3017,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[645,648,647,646,649],"class_list":["post-3005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-collection-hierarchy","tag-collection-interfaces","tag-framework","tag-java-collections","tag-methods-of-collection-interface"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3005"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3005\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3017"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}