ArrayList: A resizable array implementation in Java Collections. Allows dynamic size changes.Example: import java.util.ArrayList;ArrayList list = new ArrayList();list.add("Apple");list.add("Banana");list.add("Cherry");System.out.println(list);list.remove(1);System.out.println(list);for (String fruit : list) { System.out.println(fruit);}
LinkedList: A doubly-linked list implementation of the List interface. Efficient for insertions and deletions.Example: import java.util.LinkedList;LinkedList list = new LinkedList();list.add(10);list.add(20);list.addFirst(5);list.addLast(30);System.out.println(list);list.remove(2);System.out.println(list);
HashMap: A map-based collection that stores key-value pairs. Provides constant-time complexity for basic operations.Example: import java.util.HashMap;HashMap map = new HashMap();map.put(1, "John");map.put(2, "Alice");map.put(3, "Bob");System.out.println(map);System.out.println("Key 2: " + map.get(2));map.remove(1);System.out.println(map);for (int key : map.keySet()) { System.out.println(key + " -> " + map.get(key));}
HashSet: A set-based collection that stores unique elements. No duplicate values allowed.Example: import java.util.HashSet;HashSet set = new HashSet();set.add("Apple");set.add("Banana");set.add("Cherry");set.add("Apple"); // DuplicateSystem.out.println(set);set.remove("Banana");System.out.println(set);for (String fruit : set) { System.out.println(fruit);}
TreeMap: A map-based collection that stores key-value pairs in sorted order.Example: import java.util.TreeMap;TreeMap map = new TreeMap();map.put(3, "Alice");map.put(1, "John");map.put(2, "Bob");System.out.println(map);map.remove(3);System.out.println(map);for (int key : map.keySet()) { System.out.println(key + " -> " + map.get(key));}
Stack: A last-in-first-out (LIFO) data structure. Allows push, pop, and peek operations.Example: import java.util.Stack;Stack stack = new Stack();stack.push(10);stack.push(20);stack.push(30);System.out.println(stack);System.out.println("Peek: " + stack.peek());System.out.println("Pop: " + stack.pop());System.out.println(stack);
Queue: A first-in-first-out (FIFO) data structure. Common implementations include LinkedList and PriorityQueue.Example: import java.util.LinkedList;Queue queue = new LinkedList();queue.add("Task1");queue.add("Task2");queue.add("Task3");System.out.println(queue);System.out.println("Poll: " + queue.poll());System.out.println(queue);
PriorityQueue: A queue that retrieves elements based on priority.Example: import java.util.PriorityQueue;PriorityQueue pq = new PriorityQueue();pq.add(30);pq.add(10);pq.add(20);System.out.println(pq);System.out.println("Poll: " + pq.poll());System.out.println(pq);
Iterators: Used for traversing collections.Example: import java.util.ArrayList;import java.util.Iterator;ArrayList list = new ArrayList();list.add("Apple");list.add("Banana");list.add("Cherry");Iterator iterator = list.iterator();while (iterator.hasNext()) { System.out.println(iterator.next());}
Arrays: A collection of elements of the same type, stored in contiguous memory locations. Used to store fixed-size sequential elements.Example: int[] arr = {1, 2, 3, 4, 5};for (int i : arr) { System.out.println(i);}System.out.println("Length: " + arr.length); Multi-Dimensional Arrays: int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};for (int[] row : matrix) { for (int […]