In Java 8+, when does a HashMap bucket turn into a red-black tree?
TREEIFY_THRESHOLD is 8 and MIN_TREEIFY_CAPACITY is 64; with a smaller table, HashMap resizes instead.
HashMap internals, choosing collections, iterators, lambdas, streams and collectors.
Pick an answer to see instantly whether you're right, with an explanation. Questions are shuffled every attempt.
TREEIFY_THRESHOLD is 8 and MIN_TREEIFY_CAPACITY is 64; with a smaller table, HashMap resizes instead.
Threshold = capacity × load factor = 16 × 0.75 = 12.
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
for (String s : list) {
if (s.equals("a")) list.remove(s);
}Structural modification during for-each iteration trips the fail-fast iterator. Use removeIf or Iterator.remove().
List<Integer> nums = List.of(1, 2, 3);
nums.add(4);List.of returns an immutable list; mutators throw at runtime.
floorKey / ceilingKey?TreeMap is a red-black tree implementing NavigableMap, with O(log n) operations.
ArrayDeque is faster than LinkedList and Stack (which is synchronized and legacy).
LinkedHashMap constructor argument enables LRU-style ordering?With access order, get moves an entry to the end; override removeEldestEntry to evict.
Stream.of("a", "bb", "ccc")
.peek(s -> System.out.print(s + " "))
.filter(s -> s.length() > 1)
.findFirst();Streams are lazy and process elements one at a time. findFirst short-circuits after "bb" passes the filter, so "ccc" is never peeked.
Stream.of("a", "b").map(String::toUpperCase);
System.out.println("done");Without a terminal operation, intermediate operations never execute.
Stream<List<String>> into Stream<String>?map would give Stream<Stream<String>>; flatMap flattens.
Collectors.toMap(Person::city, Person::name) meets two people in the same city?Without a merge function toMap throws on duplicate keys. Pass one, e.g. (a, b) -> a + "," + b.
Map<Boolean, List<T>> that always contains both keys?partitioningBy always has true and false entries; groupingBy only creates keys that occur.
int count = 0;
list.forEach(x -> count++);Captured locals must not be reassigned. Use list.size(), a stream count(), or an AtomicInteger.
orElse(x) and orElseGet(() -> x) on an Optional with a value?Arguments are evaluated before a method is called, so an expensive default passed to orElse always runs.
Blocking I/O on the shared common ForkJoinPool starves the whole JVM. Use virtual threads or a dedicated executor for I/O fan-out.
String::compareToIgnoreCase is used as a Comparator<String>. What kind of method reference is it?The first lambda parameter becomes the receiver: (a, b) -> a.compareToIgnoreCase(b).
You scored 0 out of 16.