The robust Java Collections Framework (JCF) API offers a number of data structures for effective data processing, manipulation, and storage. List, Set, and Map are the three JCF interfaces that are most frequently used. Writing scalable and effective Java programs requires an understanding of these basic collection types.
In this blog, we’ll explore:
- What Lists, Sets, and Maps are
- How they differ from each other
- Common implementations of each
- When to use which collection
1. Lists in Java
In Java, an ordered collection that permits duplicate elements is called a list. Lists give elements indexed access and preserve the insertion order.
Typical List Implementations:
- ArrayList: An implementation of a resizable array.
- LinkedList: An implementation of a doubly linked list.
- Vector is a synchronized ArrayList that isn’t used very often these days.
Example: Using ArrayList
import java.util.*;
public class ListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Banana"); // Duplicates are allowed
System.out.println("List: " + list);
}
}
Output:
List: [Apple, Banana, Cherry, Banana]
When to Use a List?
- When you need to maintain the order of elements.
- When duplicates are required.
- When you need fast access by index (ArrayList) or frequent insertions/removals (LinkedList).
2. Sets in Java
In Java, a set is a collection that, with the exception of some implementations, does not permit duplicate elements and does not ensure order.
Typical Set Implementations:
- HashSet: Hashing is used; no order is preserved.
- The LinkedHashSet preserves the order of insertion.
- TreeSet: Preserves the sorted order, or natural ordering.
Example: Using HashSet
import java.util.*;
public class SetExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
set.add("Banana"); // Duplicate, will not be added
System.out.println("Set: " + set);
}
}
Output (Order may vary):
Set: [Apple, Banana, Cherry]
When to Use a Set?
- When you need unique elements only.
- When order is not important (HashSet) or ordering is required (TreeSet).
- When frequent lookups are needed (HashSet provides O(1) lookup time).
3. Maps in Java
In Java, a collection that holds key-value pairs is called a map. Values can be replicated, but each key needs to be distinct.
Typical Map Implementations:
- Unordered key-value storage is known as a hash map.
- The LinkedHashMap preserves the order of insertion.
- TreeMap: Sorts and stores keys.
Example: Using HashMap
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Cherry");
map.put(2, "Mango"); // Overwrites previous value
System.out.println("Map: " + map);
}
}
Output (Order may vary):
Map: {1=Apple, 2=Mango, 3=Cherry}
When to Use a Map?
- When data needs to be stored in key-value pairs.
- When fast retrieval based on keys is needed (HashMap offers O(1) lookup time).
- When ordered traversal of keys is required (LinkedHashMap or TreeMap).
Comparison Table: List vs Set vs Map
Feature | List | Set | Map |
---|---|---|---|
Allows Duplicates | Yes | No | Keys: No, Values: Yes |
Maintains Order | Yes (Insertion Order) | No (Except LinkedHashSet) | No (Except LinkedHashMap) |
Access Time Complexity | O(1) (ArrayList), O(N) (LinkedList) | O(1) (HashSet), O(log N) (TreeSet) | O(1) (HashMap), O(log N) (TreeMap) |
Key-Value Pairs | No | No | Yes |
Conclusion
In Java development, choosing the appropriate data structure requires an understanding of the distinctions between List, Set, and Map. Each has specific applications:
- When it’s crucial to keep things organized and permit duplicates, use lists.
- When unique elements are needed, use Set.
- When storing key-value pairs is required, use Map.
You can create Java apps that are more scalable and efficient by becoming proficient with these collections.
It might be helpful:
Is SAP ABAP a High Paying Job?