Java Map computeIfAbsent(): Multimaps, Caches and null Rules
Learn how Java Map computeIfAbsent() works, with multimap and cache examples, null and exception rules, thread safety and the putIfAbsent() difference.
Learn how Java Map computeIfAbsent() works, with multimap and cache examples, null and exception rules, thread safety and the putIfAbsent() difference.
Java ConcurrentSkipListMap keeps keys sorted for many threads. Learn skip lists, floor and ceiling keys, live range views, polling and TreeMap differences.
Find the max value in a Java Map and its key with streams, Collections.max() or one loop, and handle min values, ties, empty maps and null values safely.
Convert a map to a list, array or set in Java using keySet(), values() and entrySet(), with streams, toArray(), ordering rules and lists of records.
Convert List to Map in Java with Collectors.toMap() and groupingBy(), fix the duplicate key IllegalStateException with a merge function, keep order.
Invert a map in Java by swapping keys and values with a loop or Collectors.toMap(), handle duplicate values with groupingBy(), and use Guava BiMap.
Java HashMap guide with examples for put, get, merge, compute, iteration, keys, internals and thread safety, plus links to every HashMap how-to.
A Java Map stores unique keys with one value each. Quick put/get/merge/computeIfAbsent examples, a comparison of HashMap, LinkedHashMap, TreeMap, ConcurrentHashMap, EnumMap, Hashtable and Map.of(), and all our Map tutorials grouped by topic.
Create a Java case insensitive map with TreeMap and CASE_INSENSITIVE_ORDER, Commons CaseInsensitiveMap or Spring LinkedCaseInsensitiveMap, side by side.
Java EnumMap stores enum keys in an array by ordinal. Learn its constructors, null rules, key order, stream collectors and how it differs from HashMap.
Create a Java nested map with computeIfAbsent and streams, add, remove and iterate entries, read values safely, deep copy, flatten and choose alternatives.
TreeMap vs HashMap in Java compared on ordering, speed, null keys, memory and key equality, with Java 25 examples and a table for picking the right map.
Java IdentityHashMap compares keys with == instead of equals(). Learn how it differs from HashMap, the String and Integer traps and the Java 20 change.
Learn how Java WeakHashMap removes entries after GC clears weak keys, the String and value-reference traps, WeakHashMap vs HashMap and real use cases.
Java TreeMap keeps keys sorted in a red-black tree. Learn comparators, floorKey(), ceilingKey(), subMap(), headMap(), tailMap() views and null key rules.
Java LinkedHashMap keeps keys in insertion or access order. See SequencedMap methods from Java 21 and build an LRU cache with removeEldestEntry().
Learn to deep copy a HashMap in Java with copy constructors, streams and serialization, and compare shallow copies with new HashMap, clone() and copyOf().
Compare ConcurrentHashMap vs synchronizedMap in Java by locking, iteration, null handling and atomic updates, and pick the right map for shared data.
Merge two maps in Java with putAll(), putIfAbsent(), merge() or Collectors.toMap(), and control duplicate keys, sums, list values and unchanged inputs.
Compare two maps in Java with equals(), keySet() and value counts, list added, removed and changed entries, and build a full diff with Guava.
Learn to convert JSON to Map and convert a Map to JSON string using Jackson 2. Given example uses Jackson ObjectMapper for conversions.
Learn how ConcurrentHashMap works since Java 8, with atomic merge and compute methods, caching, bulk operations, newKeySet, sizing and common mistakes.
A good HashMap key overrides equals() and hashCode() together, uses fields that never change and spreads its hash codes well. Records, a mutable key that gets lost, collisions, Comparable keys, EnumMap and Lombok, with tested Java 25 code.
Learn to iterate HashMap in Java with entrySet, forEach, keySet and streams, remove entries safely, and compare each way with JMH results on JDK 25.
Learn how HashMap works internally in Java with hashing, bucket index, collisions, Java 8 tree bins, resizing and why equal keys need equal hash codes.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.