Real Default Methods in Java Collections API
Fast recall
Quick revision
Core answer
forEach() to Iterable; removeIf(), stream(), and parallelStream() to Collection; replaceAll() and sort() to List; and methods such as computeIfAbsent() and merge() to Map. Default implementations allowed existing types to gain these APIs with broad source and binary compatibility, although new default-method conflicts or behavior assumptions can still require attention.Whether the candidate connects default methods to actual APIs they use daily, not just theory.
Not knowing that stream() and forEach() are default methods added in Java 8.
Show how removeIf() and replaceAll() replace verbose iterator-based loops with cleaner code.
Interview-ready guide
Interview answer
Java 8 added lambda- and stream-friendly defaults across several Collections Framework interfaces.
Iterable gained forEach. Collection gained removeIf, stream, and parallelStream. List gained sort and replaceAll.
Map, which is separate from Collection, gained methods such as getOrDefault, putIfAbsent, computeIfAbsent, merge, forEach, and replaceAll. The declaring interface matters because the available operations and their contracts come from that interface and its implementation. These defaults let many existing implementations inherit useful behavior instead of immediately declaring every new method.
They provide broad compatibility, not a promise of zero impact: removal may be unsupported, Map methods follow the chosen implementation's null and concurrency rules, and inherited defaults can conflict.
For example, computeIfAbsent is useful when a missing key should derive a value, but its atomicity and recursion rules depend on the Map implementation.
Real Default Methods in Java Collections API
Direct Answer
Java 8 added forEach() to Iterable; removeIf(), stream(), and parallelStream() to Collection; replaceAll() and sort() to List; and methods such as computeIfAbsent() and merge() to Map. Default implementations allowed existing types to gain these APIs with broad source and binary compatibility, although new default-method conflicts or behavior assumptions can still require attention.
Code Example
var orders = new ArrayList<Order>();
orders.add(new Order(1L, "Laptop", 1200.0, false));
orders.add(new Order(2L, "Mouse", 25.0, true));
orders.add(new Order(3L, "Keyboard", 80.0, false));
orders.add(new Order(4L, "Monitor", 300.0, true));
// forEach() - default method from Iterable
orders.forEach(order -> System.out.println(order.item()));
// removeIf() - default method from Collection
orders.removeIf(order -> order.shipped());
// stream() - default method from Collection
var total = orders.stream()
.map(Order::price)
.reduce(0.0, Double::sum);
// sort() - default method from List
orders.sort(Comparator.comparing(Order::price));
// replaceAll() - default method from List
orders.replaceAll(order ->
new Order(order.id(), order.item().toUpperCase(),
order.price(), order.shipped()));
record Order(Long id, String item, double price, boolean shipped) {}
Common Mistakes & Pitfalls
- Naming the wrong declaring interface:
forEach()was added toIterable, whilestream()andremoveIf()are onCollection;Mapis a separate branch. - Assuming all collections are mutable:
removeIf(),sort(), andreplaceAll()can throwUnsupportedOperationExceptionwhen the implementation does not support the change. - Assuming every
computeIfAbsent()is an atomic cache loader: concurrency and mapping-function guarantees depend on the Map implementation; read its contract. - Using
forEach()for external accumulation: a collector or ordinary loop often makes ownership and concurrency clearer.
Check your understanding
Answer these without looking back at the article.
- How does removeIf() solve the ConcurrentModificationException problem?
- What default methods were added to Map in Java 8?
- What is List.sort() and how does it differ from Collections.sort()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.