Java Collections Framework Notes
1. Collections Framework Hierarchy:
Iterable
Collection
/ | \
List Set Queue
^ ^ ^
ArrayList HashSet PriorityQueue
LinkedList TreeSet ArrayDeque
SortedSet
NavigableSet
Map
/ \
HashMap SortedMap
TreeMap
2. List, Set and Queue with Example:
List: Ordered, duplicates allowed
Example:
List<String> list = new ArrayList<>();
list.add("Apple"); list.add("Banana");
Set: No duplicates
Set<String> set = new HashSet<>();
set.add("Dog"); set.add("Cat");
Queue: FIFO
Queue<String> queue = new LinkedList<>();
queue.add("A"); queue.add("B");
3. LinkedList Example:
LinkedList<String> list = new LinkedList<>();
list.add("One"); list.add("Two");
4. LinkedList Methods:
add(), addFirst(), addLast(), removeFirst(), get(index)
5. HashMap Example:
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "One");
6. HashMap Methods:
put(), putIfAbsent(), remove(), containsKey(), replace()
7. ArrayList with forEach:
ArrayList<String> items = new ArrayList<>();
items.add("Pen"); items.forEach(System.out::println);
8. Differences: Array vs ArrayList vs Vector vs Stack
9. Set vs Map:
Set - only values, Map - key-value
10. Types of Set and Map:
Set: HashSet, TreeSet, LinkedHashSet
Map: HashMap, TreeMap, LinkedHashMap, Hashtable
11. Stack & Queue Example:
Stack<Integer> stack = new Stack<>();
stack.push(10); stack.pop();
Queue<String> queue = new LinkedList<>();
queue.add("A"); queue.poll();
12. TreeSet and Hashtable:
TreeSet<Integer> ts = new TreeSet<>();
ts.add(3); ts.add(1);
Hashtable<String, String> ht = new Hashtable<>();
ht.put("India", "Delhi");
End of Notes.