By default, in what order does LinkedHashMap iterate?
With accessOrder = true it uses most-recent-access order.
HashMap and TreeMap · Exercise 1 of 4
Twenty multiple-choice questions on Java maps: HashMap, LinkedHashMap and TreeMap, their methods, iteration, internals, null keys, navigation and LRU caches. Pick an option to check your answer instantly.
By default, in what order does LinkedHashMap iterate?
With accessOrder = true it uses most-recent-access order.
Which method do you override in LinkedHashMap to evict old entries automatically?
Return true when size() exceeds the capacity.
What does counts.merge(word, 1, Integer::sum) do?
The idiomatic way to count.
What does groups.computeIfAbsent(k, x -> new ArrayList<>()).add(v) do?
computeIfAbsent returns the existing or newly created value.
Which is the most efficient way to iterate over both keys and values?
keySet + get does an extra lookup per key.
A mutable object is used as a HashMap key and a field used in its hashCode is changed afterwards. What happens?
Prefer immutable keys.
Which statement about Hashtable is true?
Use HashMap, or ConcurrentHashMap for concurrency.
Which is true of Map.of("a", 1, "b", 2)?
Use new LinkedHashMap<>(…) if you need order and mutability.
Which map should several threads update concurrently?
Plain HashMap can lose updates or corrupt itself under concurrent writes.
What happens if you remove an element from map.values()?
keySet(), values() and entrySet() are views backed by the map.
Timed online tests with instant scores and explanations.