dropWhile() — drops elements while a predicate is true, then keeps the rest. skip(n) — discards exactly the first n elements regardless of their values. Think of dropWhile() as walking past houses until you find one you like, then stopping — while skip() is walking exactly 5 houses no matter what.
dropWhile():
1List<Integer> result = List.of(1, 2, 3, 4, 5).stream()2 .dropWhile(x -> x < 3)3 .toList();4// [3, 4, 5] — drops 1, 2, keeps from 3 onward
skip():
1List<Integer> result = List.of(1, 2, 3, 4, 5).stream()2 .skip(2)3 .toList();4// [3, 4, 5] — skips first 2 elements
When to use each:
1// Real-world: process log entries after a certain timestamp2List<LogEntry> entries = logEntries.stream()3 .sorted(Comparator.comparing(LogEntry::getTimestamp))4 .dropWhile(e -> e.getTimestamp().isBefore(startTime))5 .collect(Collectors.toList());67// Real-world: skip first 10 results (pagination)8List<LogEntry> entries = logEntries.stream()9 .skip(10)10 .collect(Collectors.toList());
Key difference: