Java collection framework consists of various classes that are used to store objects. These classes on the top implements the Collection interface. Some of the classes provide full implementations that can be used as it is. Others are abstract classes, which provides skeletal implementations that can be used as a starting point for creating concrete collections.
This table contains abstract and non-abstract classes that implements collection interface.
The standard collection classes are:
Class | Description |
---|---|
AbstractCollection | Implements most of the Collection interface. |
AbstractList | Extends AbstractCollection and implements most of the List interface. |
AbstractQueue | Extends AbstractCollection and implements parts of the Queue interface. |
AbstractSequentialList | Extends AbstractList for use by a collection that uses sequential rather than random access of its elements. |
LinkedList | Implements a linked list by extending AbstractSequentialList |
ArrayList | Implements a dynamic array by extending AbstractList |
ArrayDeque | Implements a dynamic double-ended queue by extending AbstractCollection and implementing the Deque interface(Added by Java SE 6). |
AbstractSet | Extends AbstractCollection and implements most of the Set interface. |
EnumSet | Extends AbstractSet for use with enum elements. |
HashSet | Extends AbstractSet for use with a hash table. |
LinkedHashSet | Extends HashSet to allow insertion-order iterations. |
PriorityQueue | Extends AbstractQueue to support a priority-based queue. |
TreeSet | Implements a set stored in a tree. Extends AbstractSet. |
This class provides implementation of an array based data structure that is used to store elements in linear order. This class implements List interface and an abstract AbstractList class. It creates a dynamic array that grows based on the elements strength.
Simple array has fixed size i.e it can store fixed number of elements but sometimes you may not know beforehand about the number of elements that you are going to store in your array. In such situations, We can use an ArrayList, which is an array whose size can increase or decrease dynamically.
ArrayList class has three constructors that can be used to create Arraylist either from empty or elements of other collection.
ArrayList() // It creates an empty ArrayList
ArrayList( Collection C ) // It creates an ArrayList that is initialized with elements of the Collection C
ArrayList( int capacity ) // It creates an ArrayList that has the specified initial capacity
Lets create an ArrayList to store string elements. See, we used add method of list interface to add elements.
import java.util.*;
class Demo
{
public static void main(String[] args)
{
ArrayList< String> al = new ArrayList< String>();
al.add("ab");
al.add("bc");
al.add("cd");
System.out.println(al);
}
}
[ab,bc,cd]
Java LinkedList class provides implementation of linked-list data structure. It used doubly linked list to store the elements.
LinkedList class has two constructors.
LinkedList() // It creates an empty LinkedList
LinkedList( Collection c) // It creates a LinkedList that is initialized with elements of the Collection c
Lets take an example to create a linked-list and add elements using add and other methods as well. See the below example.
import java.util.* ;
class Demo
{
public static void main(String[] args)
{
LinkedList< String> ll = new LinkedList< String>();
ll.add("a");
ll.add("b");
ll.add("c");
ll.addLast("z");
ll.addFirst("A");
System.out.println(ll);
}
}
[A, a, b,c, z]
ArrayList and LinkedList are the Collection classes, and both of them implements the List interface. The ArrayList class creates the list which is internally stored in a dynamic array that grows or shrinks in size as the elements are added or deleted from it. LinkedList also creates the list which is internally stored in a DoublyLinked List. Both the classes are used to store the elements in the list, but the major difference between both the classes is that ArrayList allows random access to the elements in the list as it operates on an index-based data structure. On the other hand, the LinkedList does not allow random access as it does not have indexes to access elements directly, it has to traverse the list to retrieve or access an element from the list.
Some more differences:
HashSet() //This creates an empty HashSet
HashSet( Collection C ) //This creates a HashSet that is initialized with the elements of the Collection C
HashSet( int capacity ) //This creates a HashSet that has the specified initial capacity
In this example, we are creating a HashSet that store string values. Since HashSet does not store duplicate elements, we tried to add a duplicate elements but the output contains only unique elements.
import java.util.*;
class Demo
{
public static void main(String args[])
{
HashSet<String> hs = new HashSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("A");
System.out.println(hs);
}
}
[A, B, C, D, E]
import java.util.*;
class Demo
{
public static void main(String args[])
{
LinkedHashSet<String> hs = new LinkedHashSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
[B, A, D, E, C, F]
TreeSet() //It creates an empty tree set that will be sorted in an ascending order according to the natural order of the tree set TreeSet( Collection C ) //It creates a new tree set that contains the elements of the Collection C TreeSet( Comparator comp ) //It creates an empty tree set that will be sorted according to given Comparator TreeSet( SortedSet ss ) //It creates a TreeSet that contains the elements of given SortedSet
Lets take an example to create a treeset that contains duplicate elements. But you can notice that it prints unique elements that means it does not allow duplicate elements.
import java.util.*;
class Demo{
public static void main(String args[]){
TreeSet<String> al=new TreeSet<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Ajay Ravi Vijay
PriorityQueue( ) //This constructor creates an empty queue. By default, its starting capacity is 11
PriorityQueue(int capacity) //This constructor creates a queue that has the specified initial capacity
PriorityQueue(int capacity, Comparator comp) //This constructor creates a queue with the specified capacity
and comparator
//The last three constructors create queues that are initialized with elements of Collection passed in c
PriorityQueue(Collection c)
PriorityQueue(PriorityQueue c)
PriorityQueue(SortedSet c)
Note: If no comparator is specified when a PriorityQueue is constructed, then the default comparator for the type of data stored in the queue is used. The default comparator will order the queue in ascending order. Thus, the head of the queue will be the smallest value. However, by providing a custom comparator, you can specify a different ordering scheme.
Lets take an example to create a priority queue that store and remove elements.
import java.util.*;
class Demo
{
public static void main(String args[])
{
PriorityQueue<String> queue=new PriorityQueue<String>();
queue.add("WE");
queue.add("LOVE");
queue.add("STUDY");
queue.add("TONIGHT");
System.out.println("At head of the queue:"+queue.element());
System.out.println("At head of the queue:"+queue.peek());
System.out.println("Iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("After removing two elements:");
Iterator itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
At head of the queue:LOVE At head of the queue:LOVE Iterating the queue elements: LOVE TONIGHT STUDY WE After removing two elements: TONIGHT WE