DEV Community

Dev Cookies
Dev Cookies

Posted on

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
Enter fullscreen mode Exit fullscreen mode

Core Methods

Adding Elements

list.add(10); // Appends element at the end
list.add(1, 20); // Inserts 20 at index 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Retrieving Elements

int val = list.get(0); // Retrieves the first element
Enter fullscreen mode Exit fullscreen mode

Updating Elements

list.set(0, 99); // Updates the first element to 99
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Checking Size and Emptiness

int size = list.size(); // Returns number of elements
boolean isEmpty = list.isEmpty(); // Checks if empty
Enter fullscreen mode Exit fullscreen mode

Iteration

for (Integer num : list) { System.out.println(num); }
Enter fullscreen mode Exit fullscreen mode

Conversion

Object[] arr = list.toArray(); // Converts to Object array
Integer[] intArr = list.toArray(new Integer[0]); // Converts to Integer array
Enter fullscreen mode Exit fullscreen mode

Sorting

list.sort(Comparator.naturalOrder()); // Sorts in ascending order
Enter fullscreen mode Exit fullscreen mode

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<>();
Enter fullscreen mode Exit fullscreen mode

Core Methods

Adding Elements

linkedList.add("Apple"); // Adds at end
linkedList.addFirst("Orange"); // Adds at beginning
linkedList.addLast("Banana"); // Adds at end
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Retrieving Elements

String first = linkedList.getFirst(); // Gets first element
String last = linkedList.getLast(); // Gets last element
Enter fullscreen mode Exit fullscreen mode

Checking Size and Emptiness

int size = linkedList.size();
boolean isEmpty = linkedList.isEmpty();
Enter fullscreen mode Exit fullscreen mode

Iteration

for (String item : linkedList) { System.out.println(item); }
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Retrieving Elements

int first = queue.poll(); // Retrieves and removes head element
int head = queue.peek(); // Retrieves head without removing
Enter fullscreen mode Exit fullscreen mode

Checking Size and Emptiness

int size = queue.size();
boolean isEmpty = queue.isEmpty();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Retrieving Elements

int top = stack.peek(); // Returns top element without removing
int popped = stack.pop(); // Removes and returns top element
Enter fullscreen mode Exit fullscreen mode

Checking if Empty

boolean isEmpty = stack.empty();
Enter fullscreen mode Exit fullscreen mode

Searching Elements

int position = stack.search(10); // Returns 1-based position
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)