The Collection interface is a part of the Java Collection Framework, which provides a unified architecture for storing and manipulating collection of objects. Collection
interface extends Iterable
interface. It is the root interface in the hierarchy and provides basic methods for adding, removing, sorting and manipulating elements in a collection.
Download image here
The Collection interface has several subinterfaces that specify different types of collection, each with specific characteristics:
List:An ordered collection (also known as a sequence). Lists can contain duplicate elements. Some common implementations are ArrayList, LinkedList, Vector and Stack. It supports random access and positional access methods (like get()
, set()
, add(int index, E element)
, etc.).
Set:A collection that cannot contain duplicate elements. This is implemented by classes like HashSet, LinkedHashSet, and TreeSet. Indexing(Accessing positional elements) is not possible in Set.
Queue: A collection designed for holding elements prior to processing. Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Examples include PriorityQueue and LinkedList (which also implements List).
Deque: A double-ended queue that allows elements to be added or removed from both ends. Examples include ArrayDeque and LinkedList.
List Interface
🆑ArrayList:
- 👉Implements List interface
- Provides random access to elements
- 👉Items can be accessed using index
- Fast iteration and size operations
- Inefficient for inserts and deletes, except at the end of the list
- It maintains insertion order and it is Not synchronized
- 👉The default initial capacity is 10 and it increases by 50% when the added more elements
List<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
🆑Stack
- Stack implements List interface
- 👉It follows Last In First Out (LIFO) order
- It is a sub-class of Vector class
- 👉It contains methods like
push()
,pop()
,peek()
,empty()
andsearch()
- It is non-synchronized
- 👉The default initial capacity is 10 and it is doubled when added more than the capacity
Stack<Integer> st = new Stack<>();
st.push(70);
st.push(40);
st.push(10);
st.push(30);
st.push(20);
st.pop();
st.forEach(System.out::println);
System.out.println(st.peek());
🆑LinkedList:
- Doubly-linked list implementation
- Efficient for inserts and deletes
- 👉Implements both List and Deque interfaces
- It maintains insertion order
- 👉Manipulation is faster because there is no traversal or shifting, only the reference changes
- 👉All methods of ArrayList, Stack, Vector (List) and Queue interface can be used in LinkedList
- 👉It does not have a default initial capacity
- Not synchronized
List<String> linkedList = new LinkedList<>();
linkedList.add("Apple");
linkedList.add("Banana");
🆑Vector:
- 👉Synchronized and thread-safe
- Generally slower due to synchronization overhead
- Uses a dynamic array to store elements
- 👉Support Enumeration(Legacy interface)
- It is a legacy class
- 👉The default initial capacity is 10 and it is doubled when added more than the capacity
Vector<Integer> vec = new Vector<>();
vec.addElement(40);
vec.addElement(50);
vec.addElement(10);
vec.addElement(30);
vec.forEach(System.out::println);
Set Interface
🆑HashSet:
- Backed by a hash table, HashSet stores the elements by using a hashing mechanism
- 👉HashSet contains unique elements only. It cannot have duplicates
- Stores elements in HashTable based on the order of hashcode
- 👉No guarantees on the hashcode order of elements.(So its Random order)
- Allows null elements
- 👉HashSet is the best approach for search operations
- HashSet class is non synchronized
- The initial default capacity of HashSet is 16, and the load factor is 0.75
Set<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
🆑LinkedHashSet:
- It implements the Set interface and inherits the HashSet class
- 👉Maintains a linked list of the entries in the set, in the order in which they were inserted
- It class provides all optional set operations and permits null elements
- It is non-synchronized
- 👉Does not allow duplicates
- 👉The initial default capacity of HashSet is 16, and the load factor is 0.75
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Apple");
linkedHashSet.add("Banana");
🆑TreeSet:
- It does not allow duplicates
- 👉Sorted according to the natural ordering of its elements or by a comparator provided at set creation time
- The access and retrieval times are quite fast
- It is non-synchronized
- 👉Sorted in ascending order by default
- It implements NavigableSet
- 👉Does not allow duplicates and NULL(because by default it sorts elements in ascending order using comparator. So, if there are NULL values in comparing it throws exception)
Set<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("Banana");
Queue Interface
🆑PriorityQueue:
- 👉It follows First In First Out (FIFO) order (Insertion occurs at end & removal occurs at beginning)
- It is non-synchronized and not thread safe
- 👉It does not allow NULL values
- 👉It maintains natural ordering to maintain priority heap
- It contains methods like
offer()
,remove()
,poll()
,element()
andpeek()
- Orders elements according to their natural ordering or by a comparator provided at queue construction time
- 👉The default initial capacity is 11
Queue<String> priorityQueue = new PriorityQueue<>();
priorityQueue.offer("Apple");
priorityQueue.offer("Banana");
priorityQueue.offer("Grapes");
priorityQueue.poll();
Deque Interface
🆑ArrayDeque:
- Resizable-array implementation of the Deque interface
- It maintains insertion order
- 👉It is not suitable for sorting operations
- It is non-synchronized and not thread-safe
- 👉It does not allow NULL values
- Faster than LinkedList for adding and removing elements at both ends
- It contains methods like
offer()
,offerFirst()
,offerLast()
,remove()
,poll()
,pollFirst()
,pollLast()
,element()
,peek()
,peekFirst()
,peekLast()
- 👉The default initial capacity is 16
- 👉Indexing is not possible
Deque<String> arrayDeque = new ArrayDeque<>();
arrayDeque.addFirst("Apple");
arrayDeque.offer("Mango");
arrayDeque.addLast("Banana");
The Collection interface and its various implementations provide a robust framework for working with groups of objects in Java. Understanding the differences between the various collection types, their implementations, and the scenarios for their use is crucial for effective and efficient programming.
Comment if you need more detailed version of this.👇
Feedback
Your feedback is important to us. If you have any corrections or suggestions, please feel free to share them. Thank you for reading!
Top comments (1)
LinkedList does not maintains insertion order by itself. It allows programmer to maintain insertion order using ArrayList methods.