Java Collections
• Lists, Stacks, Queues, PriorityQueues, Sets and
Maps
Agenda
• 1. Lists
• 2. Stacks
• 3. Queues
• 4. PriorityQueues
• 5. Sets
• 6. Maps
What is a Collection?
• A Collection is a group of objects known as
elements.
• Java provides Collection Framework to handle
and manipulate them.
Why Collections are Important
• Efficient data storage, searching, sorting, and
management.
Collection Interface
• The root interface in the Java Collection
hierarchy.
• Common subinterfaces include List, Set, and
Queue.
List Interface
• Stores ordered elements.
• Allows duplicates.
• Examples: ArrayList, LinkedList
ArrayList
• Resizes dynamically.
• Fast for read operations.
ArrayList Example
• ArrayList<String> list = new ArrayList<>();
• list.add("Apple");
• list.add("Banana");
LinkedList
• Doubly linked nodes.
• Fast for insertions and deletions.
LinkedList Example
• LinkedList<Integer> list = new LinkedList<>();
• list.addFirst(1);
• list.addLast(2);
Iterators
• Used to traverse collections safely.
• Supports remove() method during iteration.
Iterator Example
• Iterator<String> it = list.iterator();
• while(it.hasNext()) {
• System.out.println(it.next());
• }
When to Use Lists
• Use ArrayList for fast reads.
• Use LinkedList for fast inserts/deletes.
Stack
• LIFO - Last In, First Out.
• Use push(), pop(), peek().
Stack Example
• Stack<Integer> stack = new Stack<>();
• stack.push(1);
• stack.pop();
Stack Real Life
• Undo operations, Backtracking, Parsing.
Queue
• FIFO - First In, First Out.
• Use offer(), poll(), peek().
Queue Example
• Queue<String> queue = new LinkedList<>();
• queue.offer("A");
• queue.poll();
Queue Real Life
• Print queue, customer queue, task scheduling.
PriorityQueue
• Elements sorted based on priority (natural or
custom order).
PriorityQueue Example
• PriorityQueue<Integer> pq = new
PriorityQueue<>();
• pq.offer(3);
• pq.poll();
Use Cases for PriorityQueue
• Task scheduling, shortest path algorithm,
Huffman coding.
Set Interface
• Stores unique elements.
• Does not allow duplicates.
HashSet
• Fastest Set implementation.
• Unordered.
HashSet Example
• Set<String> set = new HashSet<>();
• set.add("A");
• set.add("A");
LinkedHashSet
• Maintains insertion order.
TreeSet
• Sorted Set implementation.
• Implements NavigableSet.
TreeSet Example
• TreeSet<Integer> ts = new TreeSet<>();
• ts.add(2);
• ts.add(1);
When to Use Sets
• HashSet: fast lookup.
• TreeSet: sorted data.
• LinkedHashSet: ordered iteration.
Map Interface
• Stores key-value pairs.
• Keys must be unique.
HashMap
• Unordered.
• Fast access using keys.
HashMap Example
• Map<String, Integer> map = new
HashMap<>();
• map.put("A", 1);
LinkedHashMap
• Maintains insertion order.
TreeMap
• Stores sorted keys.
TreeMap Example
• TreeMap<String, Integer> tm = new
TreeMap<>();
• tm.put("B", 2);
When to Use Maps
• HashMap: fast lookup.
• TreeMap: sorted keys.
• LinkedHashMap: ordered keys.
Summary
• Use appropriate collections for performance
and simplicity.
• Practice makes perfect!
Quiz / Review
• 1. What's the difference between List and Set?
• 2. When to use PriorityQueue?
Thank You
• Questions?