๐งบ Different Collection Types in Java
The Java Collections Framework (JCF) provides a set of interfaces and classes to store, manipulate, and process groups of objects efficiently.
These collections are designed to handle various types of data operations such as searching, sorting, insertion, deletion, and synchronization.
๐น 1. List
-
Definition: An ordered collection that allows duplicate elements.
-
Common Implementations:
-
ArrayListโ Fast random access, backed by a dynamic array. -
LinkedListโ Fast insertion/deletion, implemented using a doubly linked list.
-
-
Example:
๐น 2. Set
-
Definition: A collection that does not allow duplicates.
-
Common Implementations:
-
HashSetโ Unordered set, based on hash table. -
LinkedHashSetโ Maintains insertion order. -
TreeSetโ Sorted set, orders elements based on their natural order or a comparator.
-
-
Example:
๐น 3. Queue
-
Definition: A FIFO (First In, First Out) data structure used to hold elements before processing.
-
Common Implementations:
-
PriorityQueueโ Orders elements according to their priority or natural ordering. -
LinkedListโ Can also be used as a queue implementation.
-
-
Example:
๐น 4. BlockingQueue
-
Definition: A thread-safe queue used in concurrent programming, commonly for the Producer-Consumer pattern.
-
Common Implementations:
-
ArrayBlockingQueue -
LinkedBlockingQueue
-
-
Example:
๐น 5. Map
-
Definition: A collection that stores key-value pairs, where each key is unique.
-
Common Implementations:
-
HashMapโ Unordered, fast lookup by key. -
LinkedHashMapโ Maintains insertion order. -
TreeMapโ Sorted map, orders entries by key.
-
-
Example:
๐ง Summary
| Collection Type | Allows Duplicates | Ordered | Sorted | Common Implementations |
|---|---|---|---|---|
| List | โ Yes | โ Yes | โ No | ArrayList, LinkedList |
| Set | โ No | โ No | โ
(in TreeSet) | HashSet, LinkedHashSet, TreeSet |
| Queue | โ Yes | โ Yes (FIFO) | โ
(in PriorityQueue) | LinkedList, PriorityQueue |
| BlockingQueue | โ Yes | โ Yes | โ No | ArrayBlockingQueue, LinkedBlockingQueue |
| Map | โ (Unique Keys) | Depends | โ
(in TreeMap) | HashMap, LinkedHashMap, TreeMap |
โ In short:
-
List โ Ordered, allows duplicates.
-
Set โ No duplicates.
-
Queue / BlockingQueue โ FIFO, used for processing tasks.
-
Map โ Key-value pairs, unique keys.
Together, these collection types form the backbone of the Java Collections Framework, making data handling efficient, flexible, and powerful.
โ๏ธ ArrayList vs LinkedList in Java
Both ArrayList and LinkedList are implementations of the List interface in the Java Collections Framework, but they differ significantly in their internal structure, performance, and use cases.
๐งฉ Internal Implementation
-
ArrayList:
Backed by a dynamic array.
Elements are stored contiguously in memory, similar to a standard array, but the size grows automatically when needed. -
LinkedList:
Implemented as a doubly linked list.
Each element is a node that holds the data and references (prevandnext) to neighboring nodes.
โ๏ธ Performance Comparison
| Operation | ArrayList | LinkedList |
|---|---|---|
| Access (get by index) | โ O(1) โ Direct access via index | โ O(n) โ Must traverse nodes sequentially |
| Insertion (at end) | โ O(1) (amortized) | โ O(1) (add at tail) |
| Insertion (in middle) | โ O(n) โ Requires shifting elements | โ O(1) if position is known (after traversal) |
| Deletion (by index) | โ O(n) โ Elements must be shifted | โ O(1) if node reference is known |
| Memory Overhead | Low | High (extra space for pointers) |
๐ง Key Differences
-
Data Storage:
-
ArrayListโ Uses a resizable array. -
LinkedListโ Uses nodes connected via pointers.
-
-
Insertion & Deletion:
-
ArrayListโ Slower for insertions/deletions (shifting required). -
LinkedListโ Faster for insertions/deletions once node position is known.
-
-
Random Access:
-
ArrayListโ Very fast (O(1)), direct index-based access. -
LinkedListโ Slow (O(n)), must traverse nodes sequentially.
-
-
Memory Usage:
-
ArrayListโ More memory-efficient. -
LinkedListโ Higher overhead (due to node pointers).
-
๐ก When to Use
| Scenario | Recommended Collection |
|---|---|
| Frequent reads / lookups | โ
ArrayList |
| Frequent insertions / deletions | โ
LinkedList |
| Memory-sensitive applications | โ
ArrayList |
| Applications implementing FIFO / Deque logic | โ
LinkedList |
๐งพ Example
๐ Summary
| Feature | ArrayList | LinkedList |
|---|---|---|
| Internal Structure | Dynamic array | Doubly linked list |
| Random Access | โ Fast | โ Slow |
| Insertion / Deletion | โ Slower (shifting) | โ Faster (pointer update) |
| Memory Usage | โ Lower | โ Higher |
| Best Suited For | Read-heavy operations | Write-heavy operations |
โ In short:
-
Use
ArrayListfor read-intensive applications. -
Use
LinkedListfor frequent insertions or deletions.
Both are powerful, but the right choice depends on your use case and performance needs.