<div dir="ltr" style="text-align: justify;">
<p>Collections in java is a framework. It is are one of the most commonly reusable data structures. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet etc.</p>
<p>Java collections are one of the most commonly used data-structures by all Java professionals. But are you using the right collection class that would best suits your need. Most programmers usually use Vectors, <i><b>ArrayList</b></i>, <i><b>HashMap </b></i>or the <i><b>Hashtable</b></i>. There are many other collection classes available with the JDK that you can use instead of re-inventing logic to suite your needs.</p>
<p>We will be trying to understand the different types of classes and when each Collection class could be used. We wouldn&#8217;t be looking into the implementation details of any collection, for that please refer the latest Java Collection API docs.</p>
<div id="ads-id" align="center"></div>
<p><b>The Core Collection Framework Interfaces</b><br />
The core collection frameworks are depicted in the following image.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/collection-interfaces.jpg" border="0" /></div>
<p>The main type of collections are:</p>
<ul>
<li>Sets</li>
<li>Lists</li>
<li>Queues</li>
<li>Maps</li>
</ul>
<p>Maps are not an integral part of the Collection framework, but they are still considered as Collection because of their capability to store and manipulate data as collection of objects.</p>
<p>Sorted Sets and Sorted Maps are basically a sorted version of Sets and Maps.<br />
<b>Hierarchy of Collection Framework</b><br />
Let us see the hierarchy of collection framework.The<i><b> java.util package</b></i> contains all the classes and interfaces for Collection framework.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/collectionhierarchy.jpg" border="0" /></div>
<p><b>Goals for Java Collections:</b><br />
The collections framework was designed to meet several goals.</p>
<ul>
<li>The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.</li>
<li>The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.</li>
<li>Extending and/or adapting a collection had to be easy.</li>
</ul>
<p><b>Commonly used methods of Collection interface:</b><br />
There are many methods declared in the Collection interface. They are as follows:</p>
<ol>
<li><i><b>public boolean add(object element):</b></i> is used to insert an element in this collection.</li>
<li><i><b>public boolean addAll(collection c):</b></i> is used to insert the specified collection elements in the invoking collection.</li>
<li><i><b>public boolean remove(object element):</b></i> is used to delete an element from this collection.</li>
<li><i><b>public boolean removeAll(Collection c):</b></i> is used to delete all the elements of specified collection from the invoking collection.</li>
<li><i><b>public boolean retainAll(Collection c):</b></i> is used to delete all the elements of invoking collection except the specified collection.</li>
<li><b><i>public int size():</i> </b>return the total number of elements in the collection.</li>
<li><i><b>public void clear():</b></i> removes the total no of element from the collection.</li>
<li><i><b>public boolean contains(object element):</b></i> is used to search an element.</li>
<li><i><b>public boolean containsAll(collection c):</b></i> is used to search the specified collection in this collection.</li>
<li><b><i>public Iterator iterator():</i> </b>returns an iterator.</li>
</ol>
<p><b>Iterator interface</b><br />
Iterator interface provides the facility of iterating the elements in forward direction only.<br />
<b>Methods of Iterator interface</b><br />
There are only three methods in the Iterator interface. They are:</p>
<ol>
<li><i><b>public boolean hasNext() </b></i>it returns true if iterator has more elements.</li>
<li><i><b>public object next()</b></i> it returns the element and moves the cursor pointer to the next element.</li>
<li><i><b>public void remove()</b></i> it removes the last elements returned by the iterator. It is rarely used.</li>
</ol>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/arraylist-class-in-java-collection/">ArrayList class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashset-class-in-collection/">LinkedHashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashset-and-treeset/">Difference between TreeSet &; HashSet</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs HashMap</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashmap-and/">Difference between HashMap and HashTable in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<p><b><br />
</b> <b>References</b><br />
<a style="-webkit-text-stroke-width: 0px; background-color: white; color: #1155cc; font-family: arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" href="http://www.janeve.me/articles/which-java-collection-to-use" target="_blank" rel="noopener">http://www.janeve.me/articles/which-java-collection-to-use</a></p>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/difference-between-wait-and-sleep/">Previous</a> <;<; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>|| >;>;<a href="https://dineshonjava.com/arraylist-class-in-java-collection/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/arraylist-class-in-java-collection/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '469'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…