The Java Collection Framework (JCF) is a unified architecture for representing and manipulating groups of objects.
It provides:
| Interface | Description | Common Implementations |
|---|---|---|
List |
Ordered collection with duplicates allowed | ArrayList, LinkedList |
Set |
Unordered collection with no duplicates | HashSet, TreeSet |
Map |
Key-value pairs (no duplicate keys) | HashMap, TreeMap |
Queue |
Ordered for processing (FIFO, LIFO) | LinkedList, PriorityQueue |
1
2
3
4
5
6
7
8
9
10
Collection (interface)
/ | \
List Set Queue
| | |
ArrayList HashSet LinkedList
PriorityQueue
Map (interface)
|
HashMap
Key Characteristics
Common Implementations
| Class | Description |
|---|---|
ArrayList |
Fast random access, resizable array |
LinkedList |
Good for frequent insertions/deletions |
Vector |
Legacy, synchronized version of ArrayList |
Useful Methods in List
| Method | Description |
|---|---|
add(E e) |
Adds element to the end |
add(int index, E) |
Adds element at specified index |
get(int index) |
Returns element at index |
remove(int index) |
Removes element at index |
contains(Object o) |
Checks if list contains element |
size() |
Returns number of elements |
clear() |
Removes all elements |
Looping Through a List
1
2
3
for (String fruit : fruits) {
System.out.println(fruit);
}
Or using indices:
1
2
3
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
public class ListExample {
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("List: " + fruits);
System.out.println("First fruit: " + fruits.get(0));
}
}
Key Characteristics
Common Implementations
| Class | Description |
|---|---|
HashSet |
Fastest access, does not maintain order |
LinkedHashSet |
Maintains insertion order |
TreeSet |
Automatically sorted, no null allowed |
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
public class SetExample {
public static void main(String[] args) {
Set<String> cities = new HashSet<>();
cities.add("Hanoi");
cities.add("Da Nang");
cities.add("Ho Chi Minh");
cities.add("Hanoi"); // duplicate, will be ignored
System.out.println("Cities: " + cities);
}
}
// Output
// Cities: [Da Nang, Ho Chi Minh, Hanoi]
Useful Methods in Set
| Method | Description |
|---|---|
add(E e) |
Adds an element (returns false if duplicate) |
remove(Object o) |
Removes element |
contains(Object o) |
Checks if element exists |
size() |
Number of elements |
clear() |
Removes all elements |
isEmpty() |
Checks if the set is empty |
Example: Using TreeSet (Sorted)
1
2
3
4
5
6
Set<Integer> numbers = new TreeSet<>();
numbers.add(5);
numbers.add(1);
numbers.add(3);
System.out.println(numbers); // [1, 3, 5] — sorted automatically
Looping Through a Set
1
2
3
for (String city : cities) {
System.out.println(city);
}
Use Set
| Use Case | Recommended |
|---|---|
| Ensure uniqueness (no duplicates) | Yes |
| Need fast lookups (check existence) | Yes |
| Want sorted order | Use TreeSet |
| Need order of insertion | Use LinkedHashSet |
Key Characteristics
Common Implementations
| Class | Description |
| ————— | ——————————————– |
| HashMap | Unordered, allows one null key |
| LinkedHashMap | Maintains insertion order |
| TreeMap | Automatically sorted by keys, no null keys |
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
ages.put("Charlie", 22);
ages.put("Alice", 28); // Key already exists, value is updated
System.out.println("Alice's age: " + ages.get("Alice")); // 28
System.out.println("All entries: " + ages);
}
}
Useful Methods in Map
| Method | Description |
|---|---|
put(K key, V value) |
Adds or updates entry |
get(Object key) |
Retrieves value for the key |
remove(Object key) |
Deletes entry by key |
containsKey(Object k) |
Checks if key exists |
containsValue(Object v) |
Checks if a value exists |
keySet() |
Returns a Set of all keys |
values() |
Returns a Collection of all values |
entrySet() |
Returns a Set of key-value pairs |
Looping Through a Map
1
2
3
for (Map.Entry<String, Integer> entry : ages.entrySet()) {
System.out.println(entry.getKey() + " is " + entry.getValue() + " years old.");
}
Example: Sorted Map (TreeMap)
1
2
3
4
5
6
Map<Integer, String> idNames = new TreeMap<>();
idNames.put(3, "C");
idNames.put(1, "A");
idNames.put(2, "B");
System.out.println(idNames); // Sorted by keys: {1=A, 2=B, 3=C}
Use Map
| Use Case | Recommended Implementation |
|---|---|
| Fast lookups by unique key | HashMap |
| Maintain insertion order | LinkedHashMap |
| Automatically sorted keys | TreeMap |
Java provides the Collections class for sorting, searching, shuffling, etc.
1
2
3
List<Integer> nums = Arrays.asList(3, 1, 4, 1, 5);
Collections.sort(nums);
System.out.println(nums); // [1, 1, 3, 4, 5]
Collections use generics to ensure type safety:
1
2
3
List<String> strings = new ArrayList<>();
strings.add("hello");
// strings.add(123); // compile-time error
| Feature | List |
Set |
Map |
|---|---|---|---|
| Allows Duplicates | Yes | - | - (keys) |
| Maintains Order | Yes (index) | - (unless TreeSet) |
- (unless LinkedHashMap) |
| Access by Index | Yes | - | - |
| Key-Value Store | - | - | Yes |