If you’ve been working with Java, you’ve probably come across collections. Java Collections Framework (JCF) is a powerful library that provides various data structures to manage and manipulate data efficiently. When I first started learning Java, understanding collections was a game-changer. I no longer had to manually create arrays with fixed sizes or write complex logic to manage data. Instead, I could use Lists, Sets, and Maps – each designed for different use cases. In this article, I’ll share my experience and break down these collection types in simple terms.
Lists: Ordered and Duplicates Allowed
A List in Java is an ordered collection that allows duplicate elements. This makes it perfect when you need to maintain the sequence of items and allow multiple occurrences of the same value. The most commonly used implementations of List are:
- ArrayList
- Uses a dynamic array to store elements.
- Provides fast read operations (O(1) for get()).
- Slower for insertions and deletions in the middle since elements need to shift.
Example:
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add(“Alice”);
names.add(“Bob”);
names.add(“Charlie”);
System.out.println(names);
}
}
- LinkedList
- Uses a doubly linked list under the hood.
- Fast insertions and deletions (O(1)), but slower access time compared to ArrayList (O(n)).
Use case: If you frequently add/remove elements from the middle, use LinkedList. Otherwise, stick with ArrayList for better performance.
To learn Java collections in details, you can enroll in the best Java classes in Mumbai or Java Courses in Mumbai.
Sets: Unique Elements Only
A Set is a collection that doesn’t allow duplicate values. This is useful when you want to store unique items like IDs or usernames. The main implementations are:
- HashSet
- Uses a hash table for storage.
- Offers constant-time performance (O(1)) for basic operations.
- Does not maintain insertion order.
Example:
import java.util.HashSet;
public class SetExample {
public static void main(String[] args) {
HashSet<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10); // Duplicate, will not be added
System.out.println(numbers);
}
}
- LinkedHashSet
- Maintains insertion order.
- Slightly slower than HashSet due to additional ordering mechanism.
- TreeSet
- Stores elements in sorted order.
- Uses a Red-Black tree for storage.
- Slower (O(log n) time complexity) but useful when sorting is required.
Maps: Key-Value Pairs
A Map is a collection that stores data in key-value pairs. Each key is unique, but values can be duplicated. Maps are widely used for things like caching, storing configurations, and quick lookups.
- HashMap
- Stores keys and values in a hash table.
- Allows null keys and multiple null values.
- Offers constant-time performance (O(1)) for basic operations.
Example:
import java.util.HashMap;
public class MapExample {
public static void main(String[] args) {
HashMap<String, Integer> ages = new HashMap<>();
ages.put(“Alice”, 25);
ages.put(“Bob”, 30);
ages.put(“Charlie”, 35);
System.out.println(ages);
}
}
- LinkedHashMap
- Maintains insertion order.
- Slightly slower than HashMap.
- TreeMap
- Stores keys in sorted order.
- Uses a Red-Black tree internally.
- Useful when you need sorted keys but has a higher time complexity (O(log n)).
When to Use What?
Collection Type | Best For | Drawbacks |
ArrayList | Fast random access, dynamic size | Slow insertions/deletions in middle |
LinkedList | Frequent insertions/deletions | Slow random access |
HashSet | Unique elements, fast access | No ordering |
LinkedHashSet | Unique elements, maintains order | Slightly slower than HashSet |
TreeSet | Unique elements, sorted order | Slower (O(log n)) |
HashMap | Key-value pairs, fast access | No ordering |
LinkedHashMap | Key-value pairs, maintains order | Slightly slower than HashMap |
TreeMap | Key-value pairs, sorted keys | Slower than HashMap |
Final Thoughts
Java Collections can seem overwhelming at first, but once you understand their strengths and weaknesses, choosing the right one becomes easier. Personally, I use ArrayList for most cases where I need a resizable array, HashSet when I want uniqueness, and HashMap when I need quick key-value lookups. However, knowing when to use LinkedList, TreeSet, or TreeMap can save you performance headaches in larger applications.
If you’re new to Java, experiment with these collections by writing simple programs. The best way to learn is by doing, and soon enough, you’ll be comfortable picking the right collection for the job. So enroll in the best Java training institute in Mumbai to learn advanced Java.