Java Collection Framework - Full Code Examples
1. Stack
import java.util.Stack;
public class StackDS {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
// push() - Element top pe add hota hai
stack.push(10);
stack.push(20);
stack.push(30);
// peek() - Top element show karta hai bina remove kiye
System.out.println("Top element: " + stack.peek()); // 30
// pop() - Top element remove karta hai
System.out.println("Removed element: " + stack.pop()); // 30
// search(x) - Top se index find karta hai (1-based)
System.out.println("Position of 10: " + stack.search(10)); // 2
// empty() - Stack empty hai ya nahi
System.out.println("Is empty? " + stack.empty());
// size() - Total elements
System.out.println("Size: " + stack.size()); // 2
}
}
2. Simple Queue
import java.util.LinkedList;
import java.util.Queue;
public class SimpleQueueDS {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// add() - rear me add karta hai
queue.add("A");
queue.add("B");
// offer() - safe add (fail na ho to false return)
queue.offer("C");
// peek() - front value dekhata hai (null agar khali)
System.out.println("Front: " + queue.peek());
// element() - peek jaisa hai lekin exception deta hai agar empty ho
System.out.println("Element: " + queue.element());
// poll() - remove front and return (null if empty)
System.out.println("Polled: " + queue.poll());
// remove() - poll jaisa hai lekin exception deta hai
System.out.println("Removed: " + queue.remove());
// isEmpty() - true ya false return karta hai
System.out.println("Is empty? " + queue.isEmpty());
// size() - queue ka size return
System.out.println("Size: " + queue.size());
}
}
3. Circular Queue
import java.util.ArrayDeque;
public class CircularQueueDS {
public static void main(String[] args) {
ArrayDeque<Integer> cq = new ArrayDeque<>();
// offer() - rear me add
cq.offer(10);
cq.offer(20);
// offerFirst() - front me add
cq.offerFirst(5);
// offerLast() - rear me add
cq.offerLast(30);
// getFirst() and getLast() - front aur rear values
System.out.println("Front: " + cq.getFirst());
System.out.println("Rear: " + cq.getLast());
// removeFirst() and removeLast() - remove front/rear
cq.removeFirst();
cq.removeLast();
// poll() - front se remove
cq.poll();
// peek() - front value
System.out.println("Peek: " + cq.peek());
}
}
4. Priority Queue
import java.util.PriorityQueue;
public class PriorityQueueDS {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
// add/offer - insert
pq.add(40);
pq.offer(20);
pq.offer(10);
// peek - smallest element
System.out.println("Top (smallest): " + pq.peek());
// poll/remove - remove smallest
System.out.println("Polled: " + pq.poll());
// contains(x) - check element exists
System.out.println("Contains 20? " + pq.contains(20));
// size() - count elements
System.out.println("Size: " + pq.size());
}
}
5. LinkedList (DLL/SLL/CLL features)
import java.util.LinkedList;
import java.util.Iterator;
public class LinkedListDS {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
// addFirst / addLast - DLL style
list.addFirst("A");
list.addLast("C");
// add(index, element)
list.add(1, "B");
// getFirst / getLast - DLL
System.out.println("First: " + list.getFirst());
System.out.println("Last: " + list.getLast());
// remove by value, index, first, last
list.remove("B");
list.removeFirst();
list.removeLast();
// push / pop - Stack jaisa kaam
list.push("X"); // front pe add
list.pop(); // front se remove
// DLL style reverse traversal
list.add("1");
list.add("2");
Iterator<String> rev = list.descendingIterator();
System.out.print("Backward: ");
while (rev.hasNext()) {
System.out.print(rev.next() + " ");
}
}
}
6. ArrayList
import java.util.ArrayList;
public class ArrayListDS {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<>();
// add, add(index, value)
arr.add("A");
arr.add(1, "B");
// set - replace
arr.set(1, "Z");
// get - get index value
System.out.println("Element at 0: " + arr.get(0));
// remove by index/value
arr.remove("Z");
arr.remove(0);
// contains, indexOf, lastIndexOf
arr.add("C");
arr.add("D");
arr.add("C");
System.out.println("Contains D? " + arr.contains("D"));
System.out.println("First index of C: " + arr.indexOf("C"));
System.out.println("Last index of C: " + arr.lastIndexOf("C"));
// clear, isEmpty, size
arr.clear();
System.out.println("Is empty? " + arr.isEmpty());
System.out.println("Size: " + arr.size());
}
}
7. Collections Utility Class
import java.util.*;
public class CollectionsUtilDS {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(4, 2, 1, 5, 3));
// sort, reverse
Collections.sort(list);
Collections.reverse(list);
// shuffle - random order
Collections.shuffle(list);
// max, min
System.out.println("Max: " + Collections.max(list));
System.out.println("Min: " + Collections.min(list));
// binarySearch - sorted list required
Collections.sort(list);
System.out.println("Index of 3: " + Collections.binarySearch(list, 3));
// frequency
System.out.println("Freq of 2: " + Collections.frequency(list, 2));
// fill - sabko same value
Collections.fill(list, 0);
System.out.println("After fill: " + list);
}
}
8. Recursion (Sum and Factorial)
public class RecursionDS {
static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1); // Recursively add
}
static int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1); // Recursively multiply
}
public static void main(String[] args) {
System.out.println("Sum of 5: " + sum(5));
System.out.println("Factorial of 5: " + factorial(5));
}
}