Collections in java

Kamesh Shekhar Prasad
4 min readFeb 13, 2021

In this blog we are going to see about collections in java. So collection is a framework in java by which we can do many predefined task by using predefined methods in collection framework.

Array:- When we want to store more than one elements inside one reference then we use array. But array has many disadvantages. Main disadvantage is that we can’t increase size of array. In starting itself we have to declare size of array.

Collection Framework:- It contains different classes and interfaces which can be used to represent a group of individual object into a single entity.

Hierarchy of collection framework:-

Hierarchy of Collection framework
  1. Iterator Interface:- It provides iterating Elements in forward direction only.
  2. Iterable Interface:- It is the root interface for all collection class. It contains only 1 abstract method.
Iterator<T> iterator()

3.Collection Interface:- This interface is implemented by all classes in collection framework. It contains many general purpose methods like Boolean add ( Object obj), Boolean addAll ( Collection c), void clear(), etc. implemented by all sub-classes of collection interface.

4. List Interface:- List interface is child interface of collection interface. It allows duplicate values.

List<data-type> listArray= new ArrayList();
List<data-type> listLinkedList = new LinkedList();
List<data-type> listVector = new Vector();
List<data-type> listStack = new Stack();

5. ArrayList:- It implements the List interface. It is non-synchronized. We can access elements randomly. It allows duplicate values.

import java.util.*;
class JavaArrayList{
public static void main(String args[]){
ArrayList<String> list = new ArrayList<String>();
list.add("Kamesh");
list.add("arti");
iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh
arti

6. LinkedList:- This uses doubly linked list data structure to store elements. Manipulation is fast as compared to array because if we delete any element in shifting is fast. It allows duplicate elements.

import java.util.*;
class JavaLinkedList{
public static void main(String args[]){
LinkedList<String> list = new LinkedList<String>();
list.add("Kamesh");
list.add("Amarjeet");
iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh
Amarjeet

7. Vector:- It uses dynamic array to store elements. It is synchronized.

import java.util.*;
class JavaVector{
public static void main(String args[]){
Vector<String> v= new Vector<String>();
v.add("Kamesh");
v.add("Amarjeet");
iterator itr=v.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh
Amarjeet

8. Stack:- It is subclass of vector. It implements last in first out order.

import java.util.*;
class JavaStack{
public static void main(String args[]){
Stack<String> v= new Stack<String>();
v.push("Kamesh");
v.push("Amarjeet");
v.pop();
iterator itr=v.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh

9. Queue Interface:- It maintains FIFO principle.It maintains ordered list to hold elements. There are various classes like PriorityQueue, Deque and ArrayDeque which implements the Queue interface.

Queue<String> Priority=new PriorityQueue();
Queue<String> Array=new ArrayDeque();

10. Priority Queue:- This class implemetns the Queue interface. It holds elements which are to be processed by their priorities. Null value is not allowed.

import java.util.*;
class JavaPriorityQueue{
public static void main(String args[]){
PriorityQueue<String> queue= new PriorityQueue<String>();
queue.add("Kamesh");
queue.add("Amarjeet");
iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Amarjeet
Kamesh

11. Deque interface:- Here we can add and remove elements from both sides. It’s full form is double ended queue.

Deque d = new ArrayDeque();

12. ArrayDeque:- This class implements Deque interface. We can add and remove elements from both sides. It is faster than ArrayList.

import java.util.*;
class JavaArrayDeque{
public static void main(String args[]){
Deque<String> deque= new ArrayDeque<String>();
deque.add("Kamesh");
deque.add("Amarjeet");
iterator itr=deque.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh
Amarjeet

13. Set Interface:- It represents unordered list of elements which doesn’t allow duplicate values. It allows at most one null value. Set is implemented by HashSet, LinkedHashSet, and TreeSet.

Set<data-type> set1 = new HashSet<data-type>();
Set<data-type> set2 = new LinkedHashSet<data-type>();
Set<data-type> set3 = new TreeSet<data-type>();

14. HashSet:- It implements set interface. It uses hash table for storage. It contains unique items.

import java.util.*;
class JavaHashSet{
public static void main(String args[]){
HashSet<String> set= new HashSet<String>();
set.add("Kamesh");
set.add("Amarjeet");
iterator itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh
Amarjeet

15. LinkedHashSet:- It represents linked list representation of set interface. It contains unique elements and maintain insertion order.

import java.util.*;
class JavaLinkedHashSet{
public static void main(String args[]){
LinkedHashSet<String> set= new LinkedHashSet<String>();
set.add("Kamesh");
set.add("Amarjeet");
iterator itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

16. SortedSet Interface:- It’s elements are arranged in increasing order.

SortedSet<data-type> set = new TreeSet();

17. TreeSet:- It implements the set interface that uses a tree for storage. It contains unique elements. The access time is fast.

import java.util.*;
class JavaTreeSet{
public static void main(String args[]){
TreeSet<String> set= new TreeSet<String>();
set.add("Kamesh");
set.add("Amarjeet");
iterator itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

Output:-

Kamesh
Amarjeet

Conclusion

So, in short collection framework provide many predefined methods that we can use and save our time. For more details you can see Official docs.

--

--