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.

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.

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

Output:-

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.

Output:-

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

Output:-

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

Output:-

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.

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.

Output:-

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

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

Output:-

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.

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

Output:-

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

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

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

Output:-

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.