Lesson 36
Java Collections Framework
JCF provides ready-to-use data structures. List, Set, Queue, Map — all built-in.
Java Track
Save progress as you learn.
61 lessons
Lesson content
JCF stores and processes object groups efficiently.
Key Terms
- Collection: Object storing a group of elements.
- List: Ordered, allows duplicates.
- Set: No duplicates allowed.
- Queue: FIFO order processing.
- Map: Key-value pair storage.
Framework Hierarchy
Iterable
│
Collection
├── List → ArrayList, LinkedList, Vector
├── Set → HashSet, LinkedHashSet, TreeSet
└── Queue → PriorityQueue, LinkedList
Map → HashMap, LinkedHashMap, TreeMapExample: ArrayList
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple");
System.out.println(fruits);
}
}[Apple, Banana, Apple]Example: HashSet
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple");
System.out.println(fruits);
}
}[Apple, Banana]Example: PriorityQueue
import java.util.PriorityQueue;
public class QueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> queue = new PriorityQueue<>();
queue.add(30);
queue.add(10);
queue.add(20);
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}10
20
30Example: HashMap
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> students = new HashMap<>();
students.put(101, "John");
students.put(102, "Alice");
students.put(103, "David");
System.out.println(students);
}
}{101=John, 102=Alice, 103=David}Iterating Collections
for (String name : names) {
System.out.println(name);
}Collection vs Collections
- Collection: Interface for grouping objects.
- Collections: Utility class with sort, search methods.
FAQ
1. What is JCF?
Interfaces and classes for managing object groups.
2. List vs Set?
List allows duplicates. Set stores unique only.
3. HashMap vs HashSet?
HashMap stores key-value pairs. HashSet stores values only.
JCF provides built-in data structures.
List allows duplicates, Set does not.
Queue follows FIFO order.
Map stores key-value pairs.
ArrayList — fast access, allows duplicates.
HashMap — fast key-value lookup.
Use interfaces, not implementation classes.
Where Java is used
Java in real-world development
List
Ordered, allows duplicates. ArrayList for fast access, LinkedList for fast insert.
Set
No duplicates. HashSet unordered, TreeSet sorted.
Queue
FIFO processing. PriorityQueue sorts by natural order.
Map
Key-value pairs. Keys unique. HashMap fast, TreeMap sorted.
Collection vs Collections
Collection is interface. Collections is utility class with sort/search.
Best Practice
Use List<String> not ArrayList<String>. Program to interface.