{"id":376,"date":"2018-02-10T13:17:21","date_gmt":"2018-02-10T13:17:21","guid":{"rendered":"http:\/\/www.tco-team.com\/blog\/?p=376"},"modified":"2018-02-28T12:35:12","modified_gmt":"2018-02-28T12:35:12","slug":"the-collections-framework-in-java","status":"publish","type":"post","link":"https:\/\/tco.am\/blog\/the-collections-framework-in-java\/","title":{"rendered":"The Collections framework in Java"},"content":{"rendered":"<p>Today\u2019s topic is a little bit technical as it is related to Java developers. We are going to speak about Java and its frameworks like Collections, the Map interface, Java TreeMap class, etc. Our team continues to study interesting and essential features in web and mobile development, so let\u2019s continue!<\/p>\n<p style=\"float: right;\"><!--more--><\/p>\n<h3>Basic concepts<\/h3>\n<p><strong>Collections in java<\/strong>\u00a0is a framework that provides an architecture to store and manipulate the group of objects. The Java Collection Framework is divided into two interfaces: Collection and Map. These interfaces isolate all collections that enter the two types of data boxes: simple sets of elements and sets of &#8220;key&#8221;. Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-390 size-large\" src=\"http:\/\/www.tco-team.com\/blog\/wp-content\/uploads\/2018\/02\/class-and-interface-hierarchy-800x450.png\" alt=\"\" width=\"750\" height=\"422\" srcset=\"https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/class-and-interface-hierarchy-800x450.png 800w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/class-and-interface-hierarchy-150x84.png 150w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/class-and-interface-hierarchy-750x422.png 750w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/class-and-interface-hierarchy-768x432.png 768w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/class-and-interface-hierarchy.png 1280w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<h5>Methods of Collection interface<\/h5>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><strong>boolean add(Object element)<\/strong> <em>is used to insert an element in this collection.<\/em><\/li>\n<li><strong>boolean addAll(Collection c)<\/strong> <em>is used to insert the specified collection elements in the invoking collection.<\/em><\/li>\n<li><strong>boolean remove(Object element)<\/strong> <em>is used to delete an element from this collection.<\/em><\/li>\n<li><strong>boolean removeAll(Collection c)<\/strong> <em>is used to delete all the elements of specified collection from the invoking collection.<\/em><\/li>\n<li><strong>boolean retainAll(Collection c)<\/strong> <em>is used to delete all the elements of invoking collection except the specified collection.<\/em><\/li>\n<li><strong>int size()<\/strong> <em>return the total number of elements in the collection.<\/em><\/li>\n<li><strong>void clear()<\/strong> <em>removes the total no of element from the collection.<\/em><\/li>\n<li><strong>boolean contains(Object element)<\/strong> <em>is used to search an element.<\/em><\/li>\n<li><strong>boolean containsAll(Collection c)<\/strong> <em>is used to search the specified collection in this collection.<\/em><\/li>\n<li><strong>Iterator iterator()<\/strong> <em>returns an iterator.<\/em><\/li>\n<li><strong>Object[] toArray()<\/strong> <em>converts collection into array.<\/em><\/li>\n<li><strong>boolean isEmpty()<\/strong> <em>checks if collection is empty.<\/em><\/li>\n<li><strong>boolean equals(Object element)<\/strong> <em>matches two collection.<\/em><\/li>\n<li><strong>int hashCode()<\/strong> <em>returns the hashcode number for collection.<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h4>How ArrayList Works Internally in Java<\/h4>\n<p>ArrayList is a resizable array implementation of the List interface i.e. ArrayList grows dynamically as the elements are added to it.<\/p>\n<p>If the size of the current elements (including the new element to be added to the ArrayList) is greater than the maximum size of the array then increase the size of array. But the size of the array can not be increased dynamically. So, what happens internally is, a new Array is created and the old array is copied into the new array.<\/p>\n<h4>Java LinkedList class<\/h4>\n<p>Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are:<\/p>\n<ul>\n<li>Java LinkedList class can contain duplicate elements.<\/li>\n<li>Java LinkedList class maintains insertion order.<\/li>\n<li>Java LinkedList class is non synchronized.<\/li>\n<li>In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.<\/li>\n<li>Java LinkedList class can be used as list, stack or queue.<\/li>\n<\/ul>\n<h4>Doubly Linked List<\/h4>\n<p>In case of doubly linked list, we can add or remove elements from both side.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-386 size-full\" src=\"http:\/\/www.tco-team.com\/blog\/wp-content\/uploads\/2018\/02\/image002.png\" alt=\"\" width=\"551\" height=\"83\" srcset=\"https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/image002.png 551w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/image002-150x23.png 150w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/p>\n<h4>The Map Interface<\/h4>\n<p>A\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\" rel=\"noopener\">Map<\/a>\u00a0is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical\u00a0<em>function<\/em>\u00a0abstraction. The\u00a0Map\u00a0interface includes methods for basic operations (such as\u00a0put,\u00a0get,\u00a0remove,\u00a0containsKey,\u00a0containsValue,\u00a0size, and\u00a0empty), bulk operations (such as\u00a0putAll\u00a0and\u00a0clear), and collection views (such as\u00a0keySet,\u00a0entrySet, and\u00a0values).The Java platform contains three general-purpose\u00a0Map\u00a0implementations:\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/HashMap.html\" target=\"_blank\" rel=\"noopener\">HashMap<\/a>,\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/TreeMap.html\" target=\"_blank\" rel=\"noopener\">TreeMap<\/a>, and\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/LinkedHashMap.html\" target=\"_blank\" rel=\"noopener\">LinkedHashMap<\/a>. Their behavior and performance are precisely analogous to\u00a0HashSet,\u00a0TreeSet, and\u00a0LinkedHashSet, as described in\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/collections\/interfaces\/set.html\" target=\"_blank\" rel=\"noopener\">The Set Interface<\/a>\u00a0section.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-380\" src=\"http:\/\/www.tco-team.com\/blog\/wp-content\/uploads\/2018\/02\/image003.png\" alt=\"\" width=\"428\" height=\"246\" srcset=\"https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/image003.png 428w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/image003-150x86.png 150w\" sizes=\"(max-width: 428px) 100vw, 428px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-391 size-large\" src=\"http:\/\/www.tco-team.com\/blog\/wp-content\/uploads\/2018\/02\/map-interface-1-800x450.png\" alt=\"\" width=\"750\" height=\"422\" srcset=\"https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/map-interface-1-800x450.png 800w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/map-interface-1-150x84.png 150w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/map-interface-1-750x422.png 750w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/map-interface-1-768x432.png 768w, https:\/\/tco.am\/blog\/wp-content\/uploads\/2018\/02\/map-interface-1.png 1280w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<h4><strong>Java TreeMap class<\/strong><\/h4>\n<p>Java TreeMap class implements the Map interface by using a tree. It provides an efficient means of storing key\/value pairs in sorted order.<\/p>\n<p>The important points about Java TreeMap class are:<\/p>\n<ul>\n<li>A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.<\/li>\n<li>It contains only unique elements.<\/li>\n<li>It cannot have null key but can have multiple null values.<\/li>\n<li>It is same as HashMap instead maintains ascending order.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Today\u2019s topic is a little bit technical as it is related to Java developers. We are going to speak about Java and its frameworks like Collections, the Map interface, Java TreeMap class, etc. Our team continues to study interesting and essential features in web and mobile development, so let\u2019s continue!<\/p>\n","protected":false},"author":5,"featured_media":384,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72,37],"tags":[105,106,104,107,108],"_links":{"self":[{"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/posts\/376"}],"collection":[{"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":11,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":393,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/posts\/376\/revisions\/393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/media\/384"}],"wp:attachment":[{"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tco.am\/blog\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}