The Collection interface is a part of the Java Collections Framework, which provides a unified architecture for storing and manipulating collections of objects. It is the root interface in the hierarchy and provides basic methods for adding, removing, and inspecting elements in a collection.
Download image here
The Collection interface has several subinterfaces that specify different types of collections, each with specific characteristics:
List:An ordered collection (also known as a sequence). Lists can contain duplicate elements. Some common implementations are ArrayList, LinkedList, and Vector.
Set:A collection that cannot contain duplicate elements. This is implemented by classes like HashSet, LinkedHashSet, and TreeSet.
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:
- Provides random access to elements.
- Fast iteration and size operations.
- Inefficient for inserts and deletes, except at the end of the list.
- Not synchronized.
List<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
LinkedList:
- Doubly-linked list implementation.
- Efficient for inserts and deletes.
- Implements both List and Deque interfaces.
- 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)
List<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
Set Interface
HashSet:
- Backed by a hash table.
- 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.
- Does not allow duplicates
Set<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
LinkedHashSet:
- Maintains a linked list of the entries in the set, in the order in which they were inserted.
- Provides predictable iteration order.
- Does not allow duplicates
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Apple");
linkedHashSet.add("Banana");
TreeSet:
- Sorted according to the natural ordering of its elements or by a comparator provided at set creation time.
- Sorted in ascending order By default.
- 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:
- An unbounded priority queue based on a priority heap.
- Orders elements according to their natural ordering or by a comparator provided at queue construction time.
Queue<String> priorityQueue = new PriorityQueue<>();
priorityQueue.add("Apple");
priorityQueue.add("Banana");
LinkedList:
- Implements both List and Deque interfaces.
- Can be used as a queue by utilizing methods like offer, poll, and peek.
Queue<String> linkedQueue = new LinkedList<>();
linkedQueue.offer("Apple");
linkedQueue.offer("Banana");
Deque Interface
ArrayDeque:
- Resizable-array implementation of the Deque interface.
- Not thread-safe.
- Faster than LinkedList for adding and removing elements at both ends.
Deque<String> arrayDeque = new ArrayDeque<>();
arrayDeque.addFirst("Apple");
arrayDeque.addLast("Banana");
LinkedList:
- Doubly-linked list implementation of the Deque interface.
Deque<String> linkedDeque = new LinkedList<>();
linkedDeque.addFirst("Apple");
linkedDeque.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 (0)