All IT Courses 50% Off
JAVA Tutorials

Collection Hierarchy

Java Collection framework

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 Hierarchy

Collection 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 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.

All IT Courses 50% Off

2] Queue: Queue contains similar elements in which new elements are added from one end, whereas removed from the other end (FIFO – First In First Out). It implements LinkedList and PriorityQueue.

3] Set: Set is used to store the collection of unique elements. 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.

4] Map: 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.

Methods of Collection Interface

The collection interface contains mainly 15 methods:

1] add(): It 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. 
2] addAll(): It adds a collection of elements to the existing collection. It will return true if the elements are already added; otherwise, it will return false.
3] clear(): It will clear or remove all the elements from the collection.
4] contains(): It 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.
5] containsAll(): It 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.
6] equals(): It will check for equality with another object. 
7] hashcode(): It will return the hash code number for the collection, and its return type is an integer.
8] isEmpty(): It will return true if a collection is empty otherwise false.
9] iterator(): It will return an iterator.
10] remove(): It will remove a specified element from the collection.
11] removeAll(): It will remove all elements from the collection.
12] retainAll(): It will remove all the elements from the collection except the specified collection.
13] size(): It will return the total number of elements present in the collection, and its return type is an integer.
14] toArray(): It 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.
15] Object[ ] toArray(): It 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.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button