Java List, Set, and Map Interfaces – Complete Guide with Examples
Learn the core Java Collections interfaces: List, Set, and Map, their differences, implementations, and how to use them to store, access, and manipulate data efficiently.
List, Set, and Map Interfaces in Java – Complete Detailed Tutorial
The Java Collections Framework (JCF) provides three main interfaces to handle collections: List, Set, and Map. Each has its own characteristics and usage.
1. List Interface
- Ordered collection (maintains insertion order)
- Allows duplicates
- Common implementations:
ArrayList,LinkedList,Vector,Stack - Supports index-based access
Key Methods:
| MethodDescription | |
| add(E e) | Adds element |
| get(int index) | Retrieves element at index |
| remove(int index) | Removes element at index |
| size() | Returns number of elements |
| contains(Object o) | Checks if element exists |
Example – Using List Interface
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate allowed
System.out.println("Fruits: " + fruits);
System.out.println("Element at index 1: " + fruits.get(1));
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
}
}
Output:
Fruits: [Apple, Banana, Orange, Apple]
Element at index 1: Banana
After removal: [Apple, Orange, Apple]
2. Set Interface
- Unordered collection (does not maintain insertion order in HashSet)
- No duplicate elements allowed
- Common implementations:
HashSet,LinkedHashSet(maintains insertion order),TreeSet(sorted)
Key Methods:
| MethodDescription | |
| add(E e) | Adds element |
| remove(Object o) | Removes element |
| contains(Object o) | Checks if element exists |
| size() | Returns number of elements |
Example – Using Set Interface
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate ignored
System.out.println("Fruits: " + fruits);
System.out.println("Contains Banana? " + fruits.contains("Banana"));
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
}
}
Output:
Fruits: [Apple, Orange, Banana]
Contains Banana? true
After removal: [Apple, Orange]
Explanation:
- Duplicates are ignored
HashSetdoes not maintain insertion order
3. Map Interface
- Stores key-value pairs
- Keys are unique, values can be duplicated
- Common implementations:
HashMap,LinkedHashMap,TreeMap
Key Methods:
| MethodDescription | |
| put(K key, V value) | Adds a key-value pair |
| get(Object key) | Retrieves value for key |
| remove(Object key) | Removes key-value pair |
| containsKey(Object key) | Checks if key exists |
| size() | Returns number of entries |
Example – Using Map Interface
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Orange");
map.put(2, "Grapes"); // key 2 updated
System.out.println("Map: " + map);
System.out.println("Value for key 2: " + map.get(2));
map.remove(3);
System.out.println("After removal: " + map);
}
}
Output:
Map: {1=Apple, 2=Grapes, 3=Orange}
Value for key 2: Grapes
After removal: {1=Apple, 2=Grapes}
Explanation:
- Keys are unique
- Values can be updated
- Maintains key-value mapping
4. Key Points
- List: ordered, allows duplicates, index-based
- Set: unordered, no duplicates, unique elements
- Map: key-value pairs, unique keys, allows duplicate values
- All interfaces provide common methods like add, remove, contains
- Can use Iterator to traverse List and Set; entrySet() for Map
5. Summary
- List, Set, and Map are the core interfaces of Collections Framework
- Chosen based on requirement: order, uniqueness, or key-value mapping
- Implementations provide optimized and ready-to-use data structures