Java Programming · Lesson 11 of 15 · Updated ·
Java Collections — ArrayList, LinkedList, HashSet and Generics
Arrays have a fixed size. The Collections Framework gives you ready, growable data structures — and knowing which to choose is a core interview topic.
1. ArrayList: the everyday list
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> cities = new ArrayList<>();
cities.add("Jaipur");
cities.add("Pune");
cities.add(0, "Delhi"); // insert at index 0
System.out.println(cities + " size=" + cities.size());
System.out.println(cities.get(1) + " " + cities.contains("Pune") + " " + cities.indexOf("Pune"));
cities.remove("Delhi");
cities.set(0, "Udaipur");
Collections.sort(cities);
System.out.println(cities);
}
}
[Delhi, Jaipur, Pune] size=3
Jaipur true 2
[Pune, Udaipur]
<String>is a generic type: this list only accepts Strings, checked at compile time.- Collections store objects, so use
Integer, notint:List<Integer>. - Declare with the interface (
List) and create with the class (ArrayList) — easy to switch later.
2. ArrayList vs LinkedList
| Operation | ArrayList (dynamic array) | LinkedList (doubly linked) |
|---|---|---|
get(i) | O(1) | O(n) |
| add at end | O(1) amortised | O(1) |
| add/remove at start or middle | O(n) — shifts elements | O(1) once you're there |
| Memory | compact | extra links per element |
Use ArrayList by default. LinkedList is rarely the faster choice in practice.
3. Sets: no duplicates
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(5, 3, 5, 1, 3, 9);
Set<Integer> hash = new HashSet<>(nums); // no order guaranteed
Set<Integer> linked = new LinkedHashSet<>(nums); // insertion order
Set<Integer> tree = new TreeSet<>(nums); // sorted
System.out.println(hash.size() + " " + linked + " " + tree);
System.out.println(tree.contains(9) + " " + ((TreeSet<Integer>) tree).first());
}
}
4 [5, 3, 1, 9] [1, 3, 5, 9]
true 1
| HashSet | LinkedHashSet | TreeSet | |
|---|---|---|---|
| Order | none | insertion | sorted |
| add / contains | O(1) | O(1) | O(log n) |
4. Iterating — and removing safely
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>(List.of(35, 80, 20, 92));
for (int m : marks) System.out.print(m + " ");
System.out.println();
marks.removeIf(m -> m < 40); // safe removal
System.out.println(marks);
Iterator<Integer> it = marks.iterator();
while (it.hasNext()) {
if (it.next() > 90) it.remove(); // also safe
}
System.out.println(marks);
}
}
35 80 20 92
[80, 92]
[80]
Removing from a list inside a for-each loop throws ConcurrentModificationException. Use
removeIfor anIterator.
5. Queue and Deque in one line each
Deque<Integer> stack = new ArrayDeque<>();→push,pop,peek(use instead of the oldStackclass)Queue<Integer> q = new ArrayDeque<>();→offer,poll,peekPriorityQueue<Integer> pq = new PriorityQueue<>();→ always gives the smallest first (a heap)
Choosing quickly
- Ordered list, access by index → ArrayList
- Unique items, fast lookup → HashSet (sorted? TreeSet)
- Key → value → HashMap (next lesson)
- Stack or queue → ArrayDeque; smallest/largest first → PriorityQueue
Common mistakes
List<int>— generics need objects:List<Integer>.- Removing elements inside a for-each loop.
list.remove(1)on aList<Integer>removes index 1, not the value 1 — uselist.remove(Integer.valueOf(1)).
Quick recap
- List (ordered, duplicates), Set (unique), Queue/Deque, Map (key-value, separate family).
- ArrayList is the default list; HashSet the default set.
- Generics give type safety; use wrapper types.
- Remove while iterating with removeIf or an Iterator.