Nearby lessons

86 of 125

Java - Queue Interface

📌 What You Will Learn
  • What the Collection Framework is and why we need it
  • List, Set, Map and Queue — the four big families
  • Choosing the right collection for the right job
  • Comparable vs Comparator
  • The internal working of HashMap (interview favourite)

Queue Interface is a core concept of the Java language. This lesson explains ueue — First In, First Out with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Queue — First In, First Out

A Queue works like a ticket counter line: the person who comes first is served first (FIFO). Use offer() to add and poll() to take out the front element.

Example01
JCode Cell
1import java.util.*;
2 
3class QueueDemo {
4 public static void main(String[] args) {
5 Queue<String> q = new ArrayDeque<>();
6 q.offer("Rahul");
7 q.offer("Priya");
8 q.offer("Anil");
9 System.out.println("Front: " + q.peek()); // look, don't remove
10 System.out.println("Served: " + q.poll()); // take front
11 System.out.println("Now front: " + q.peek());
12 }
13}
Output
Front: Rahul Served: Rahul Now front: Priya
📝 Key Takeaways
  • Collection is an interface; Collections is a utility class with static helpers.
  • List = ordered, allows duplicates. Set = no duplicates. Map = key-value pairs. Queue = FIFO.
  • ArrayList is the everyday list; HashMap is the everyday map.
  • HashMap works on hashCode + buckets; collisions chain, then become trees (Java 8+).
  • Comparable (compareTo) is the natural order; Comparator (compare) gives custom order.
  • Always override equals() and hashCode() together for your own objects.
  • Collections.sort() and Collections.reverse() are the handy helpers.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1