Mastering Java Collections: A Comprehensive Guide (Part 1)

Java Collections Framework (JCF) provides powerful data structures that simplify complex programming tasks. In this blog series, we will explore all collection types and their methods in detail, ensuring you skip none!
1. ArrayList (java.util.ArrayList
)
Overview
ArrayList
is a resizable array implementation of the List
interface. It allows random access, dynamic resizing, and provides fast read operations (O(1)) but slower insertions/deletions (O(n)).
Constructors
ArrayList<Integer> list = new ArrayList<>(); // Default
ArrayList<Integer> list2 = new ArrayList<>(20); // With initial capacity
ArrayList<Integer> list3 = new ArrayList<>(list2); // Copying another list
Core Methods
Adding Elements
list.add(10); // Appends element at the end
list.add(1, 20); // Inserts 20 at index 1
Removing Elements
list.remove(1); // Removes element at index 1
list.remove(Integer.valueOf(10)); // Removes first occurrence of 10
list.clear(); // Removes all elements
Retrieving Elements
int val = list.get(0); // Retrieves the first element
Updating Elements
list.set(0, 99); // Updates the first element to 99
Searching Elements
boolean exists = list.contains(99); // Returns true if 99 exists
int index = list.indexOf(99); // Returns index of first occurrence
int lastIndex = list.lastIndexOf(99); // Returns last occurrence
Checking Size and Emptiness
int size = list.size(); // Returns number of elements
boolean isEmpty = list.isEmpty(); // Checks if empty
Iteration
for (Integer num : list) { System.out.println(num); }
Conversion
Object[] arr = list.toArray(); // Converts to Object array
Integer[] intArr = list.toArray(new Integer[0]); // Converts to Integer array
Sorting
list.sort(Comparator.naturalOrder()); // Sorts in ascending order
2. LinkedList (java.util.LinkedList
)
Overview
A LinkedList
is a doubly linked list implementation of List
and Deque
. It allows fast insertions and deletions (O(1)) but slower access (O(n)).
Constructors
LinkedList<String> linkedList = new LinkedList<>();
Core Methods
Adding Elements
linkedList.add("Apple"); // Adds at end
linkedList.addFirst("Orange"); // Adds at beginning
linkedList.addLast("Banana"); // Adds at end
Removing Elements
linkedList.remove(); // Removes first element
linkedList.remove(1); // Removes element at index 1
linkedList.removeFirst(); // Removes first element
linkedList.removeLast(); // Removes last element
Retrieving Elements
String first = linkedList.getFirst(); // Gets first element
String last = linkedList.getLast(); // Gets last element
Checking Size and Emptiness
int size = linkedList.size();
boolean isEmpty = linkedList.isEmpty();
Iteration
for (String item : linkedList) { System.out.println(item); }
Queue Methods (Inherited from Deque)
linkedList.offer("Grapes"); // Adds element at end
String head = linkedList.poll(); // Retrieves and removes first element
String peekElement = linkedList.peek(); // Retrieves first element without removing
3. Queue (java.util.Queue
)
Overview
A Queue
follows FIFO (First-In-First-Out) principle. It is implemented by LinkedList
and PriorityQueue
.
Core Methods
Adding and Removing Elements
Queue<Integer> queue = new LinkedList<>();
queue.offer(10); // Adds 10 to the queue
queue.offer(20);
Retrieving Elements
int first = queue.poll(); // Retrieves and removes head element
int head = queue.peek(); // Retrieves head without removing
Checking Size and Emptiness
int size = queue.size();
boolean isEmpty = queue.isEmpty();
4. Stack (java.util.Stack
)
Overview
A Stack
follows LIFO (Last-In-First-Out) principle. It extends Vector
.
Core Methods
Adding and Removing Elements
Stack<Integer> stack = new Stack<>();
stack.push(10); // Pushes element onto stack
stack.push(20);
Retrieving Elements
int top = stack.peek(); // Returns top element without removing
int popped = stack.pop(); // Removes and returns top element
Checking if Empty
boolean isEmpty = stack.empty();
Searching Elements
int position = stack.search(10); // Returns 1-based position
Conclusion
This concludes Part 1 of our Java Collections deep dive. We covered ArrayList, LinkedList, Queue, and Stack, exploring all their methods with examples. Stay tuned for Part 2, where we will cover Deque, HashSet, and HashMap in detail!
Next Up: Java Collections Part 2 – Deque, HashSet, and HashMap