All the classes and interfaces of the collection framework are in java.util package. This hierarchy for the collection framework specifically mentioned the class and interface with respect to each type.
The Iterable interface is the root interface for all the collection classes because the Collection interface extends the Iterable interface, therefore, all the subclasses of Collection interface also implement the Iterable interface.
The iterable interface contains only one abstract method.
- Iterator iterator(): It returns the iterator over the elements of type T.
The iterator interface provides the facility of iterating the elements in a forward direction only.
For more detail: Java: Iterator Interface methods and Examples
The Collection interface builds the foundation for the Collection framework. The collection interface is one of the interfaces which is implemented by all the Collection framework classes. It provides common methods to implement by all the subclasses of collection interfaces.
For more detail: Java: Collection Interface methods and Examples
List interface is the subtype/child interface of the Collection interface. It stores objects/elements in list type data structure in ordered form and also allowed duplicate values.
Implementation classes for List interface are ArrayList, LinkedList, Vector, and Stack.
For Example: To instantiate List Interface with Implementation classes follow:
List list1= new ArrayList();
List list2 = new LinkedList();
List list3 = new Vector();
List list4 = new Stack();
For more detail: Java: List Interface methods and Examples
The ArrayList implements the List interface. It’s having the following features:
- ArrayList uses a dynamic array data structure to store objects and elements.
- ArrayList allows duplicate objects and elements.
- ArrayList maintains the insertion order.
- ArrayList is non-synchronized.
- ArrayList elements/objects can be accessed randomly.
For more detail: Java: ArrayList Interface methods and Examples
LinkedList implements the List interface. It’s having the following features:
- LinkedList uses a doubly linked list data structure to store elements.
- LinkedList allowed storing the duplicate elements.
- LinkedList maintains the insertion order.
- LinkedList is not synchronized.
- LinkedList manipulation is fast because no shifting is required.
For more detail: Java: LinkedList Class methods and Examples
Vector Class implements List interface. It’s having the following features:
- Vector is similar to the ArrayList class.
- Vector class uses data structure as a dynamic array to store the data elements.
- Vector is synchronized.
- Vector contains many methods that are not the part of Collection Framework.
For more detail: Java: Vector Class methods and Examples
The Stack is the subclass of the Vector class. It’s having the following features:
- Stack implements the Vector data structure with the (LIFO)last-in-first-out.
- Stack contains all of the methods of the Vector class.
- Stack also provides its methods like boolean push(), boolean peek(), boolean push(object o), which defines its features.
For more detail: Java: Stack Class methods and Examples
Queue Interface extends the Collection interface. It’s having the following features:
- Queue interface maintains the FIFO (first-in-first-out) order.
- Queue can be defined as an ordered list that is used to hold the elements which are about to be processed.
- Queue interface implemented by the various classes like PriorityQueue, Deque, and ArrayDeque.
Queue interface can be instantiated as:
Queue q1 = new PriorityQueue();
Queue q2 = new ArrayDeque();
For more detail: Java: Queue Interface methods and Examples
Here are detail about classes which implements Queue Interface.
The PriorityQueue class implements the Queue interface.
- PriorityQueue holds the elements or objects which are to be processed by their priorities.
PriorityQueue doesn’t allow null values to be stored in the queue.
For more detail: Java: PriorityQueue Class methods and Examples
Deque stands for the double-ended queue which allows us to perform the operations at both ends.interface extends the Queue interface.
- Deque extends the Queue interface.
- Deque allows remove and add the elements from both the side.
Deque d = new ArrayDeque();
For more detail: Java: Deque Interface methods and Examples
ArrayDeque class implements the Deque interface.
- ArrayDeque facilitates us to use the Deque.
- ArrayDeque allows add or delete the elements from both the ends.
- ArrayDeque is faster than ArrayList and has no capacity restrictions.
For more detail: Java: ArrayQueue Class methods and Examples
Set Interface extends Collection Interface and present in java.util package.
- Set doesn’t allow duplicate elements or objects.
- Set store elements in an unordered way.
- Set allows only one null value.
- Set is implemented by HashSet, LinkedHashSet, and TreeSet.
We can create an instantiation of Set as below:
Set s1 = new HashSet();
Set s2 = new LinkedHashSet();
Set s3 = new TreeSet();
For more detail: Java: Set Interface methods and Examples
HashSet class implements Set Interface. It’s having the following features:
- HashSet internally uses data structure like a hash table for storage.
- HashSet uses hashing technique for storage of the elements.
- HashSet always contains unique items.
For more detail: Java: HashSet Class methods and Examples
LinkedHashSet class implements Set Interface. It’s having the following features:
- LinkedHashSet store items in LinkedList.
- LinkedHashSet store unique elements.
- LinkedHashSet maintains the insertion order.
- LinkedHashSet allows null elements.
For more detail: Java: LinkedHashSet Class methods and Examples
SortedSet Interface extends Set Interface. It’s having the following features:
- SortedSet provides a total ordering on its elements.
- SortedSet elements are arranged in the increasing (ascending) order.
- SortedSet provides additional methods that inhibit the natural ordering of the elements.
The SortedSet can be instantiated as:
SortedSet set = new TreeSet();
For more detail: Java: SortedSet Interface methods and Examples
TreeSet class implements the SortedSet interface. It’s having the following features:
- TreeSet uses a tree data structure for storage.
- TreeSet also contains unique elements.
- TreeSet elements access and retrieval time is quite fast.
- TreeSet elements stored in ascending order.
For more detail: Java: TreeSet Class methods and Examples
In the collection framework, a map contains values on the basis of key and value pair. This pair is known as an entry. A map having the following features:
- Map contains unique keys.
- Map allows duplicate values.
- Map is useful to search, update or delete elements on the basis of a key.
- Map is the root interface in Map hierarchy for Collection Framework.
- Map interface is extended by SortedMap and implemented by HashMap, LinkedHashMap.
- Map implementation classes HashMap and LinkedHashMap allow null keys and values but TreeMap doesn’t allow null key and value.
- Map can’t be traversed, for traversing needs to convert into the set using method keySet() or entrySet().
For more detail: Java: Map Class methods and Examples
HashMap class implements Map interface. It’s having following features:
- HashMap uses data structure as a Hash Table.
- HashMap store values based on keys.
- HashMap contains unique keys.
- HashMap allows duplicate values.
- HashMap doesn’t maintain order.
- HashMap class allows only one null key and multiple null values.
- HashMap is not synchronized.
- HashMap initial default capacity is 16 elements with a load factor of 0.75.
For more detail:
LinkedHashMap class extends the HashMap class. It’s having the following features:
- LinkedHashMap contains values based on the key.
- LinkedHashMap contains unique elements.
- LinkedHashMap may have one null key and multiple null values.
- LinkedHashMap is not synchronized.
- LinkedHashMap maintains the insertion order.
- LinkedHashMap default initial capacity is 16 with a load factor of 0.75.
For more detail: Java: LinkedHashMap Class methods and Examples
TreeMap class implements the SortedMap interface. it’s having the following features:
- TreeMap uses data structure as red-black tree.
- TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
- TreeMap contains only unique elements.
- TreeMap doesn’t allow null keys and values.
- TreeMap is not synchronized.
- TreeMap maintains an ascending order.
For more detail: Java: TreeMap Class methods and Examples
Hashtable class implements a Map interface and extends Dictionary class to store key and values as pairs. It’s having the following features:
- HashTable store values as an array of the list where each list is known as a bucket of the node(key and value pair).
- HashTable class is in java.util package.
- Hashtable contains unique elements.
- Hashtable doesn’t allow null key or value.
- Hashtable is synchronized.
- Hashtable initial default capacity is 11 whereas the load factor is 0.75.
For more detail: Java: HashTable Class methods and Examples