Fundamentals 10 min read

Mastering Java Map: 12 Powerful Methods Every Developer Should Know

This article introduces Java's Map interface, explains the expanded API added since Java 8, and provides practical code examples for methods such as getOrDefault, forEach, replaceAll, computeIfAbsent, merge, and the immutable factories introduced in Java 9, helping developers deepen their collection framework knowledge.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Java Map: 12 Powerful Methods Every Developer Should Know

Introduction

In Java, the Map interface is more than just put and get ; it is a rich, extensible component of the collections framework used to store unique key‑value pairs. Starting with Java 8, many powerful default methods were added, and Java 9 introduced immutable factory methods.

Initial Data

<code>private static Map<String, String> map = new HashMap<>();
static {
    map.put("a", "aaa");
    map.put("b", "bbb");
}
</code>

2.1 getOrDefault

Returns the value for a given key, or a default value if the key is absent.

<code>public static void getOrDefault() {
    String value = map.getOrDefault("c", "ccc");
    System.out.println(value);
}
// Output: ccc
</code>

2.2 forEach

Executes the provided action for each entry in the map.

<code>public static void forEach() {
    map.forEach((key, value) -> System.out.println(key + "@" + value));
}
</code>

2.3 replaceAll

Replaces each value with the result of applying a given function.

<code>public static void replaceAll() {
    map.replaceAll((key, value) -> "new - " + value);
    System.out.println(map);
}
// Output: {a=new - aaa, b=new - bbb}
</code>

2.4 replace

Updates a specific entry; two overloads are shown.

<code>public static void replace() {
    map.replace("a", "new - " + map.get("a"));
    System.out.println(map);
}
// Output: {a=new - aaa, b=bbb}

public static void replace2() {
    map.replace("a", "aaa", "new - aaa");
    System.out.println(map);
}
// Output: {a=new - aaa, b=bbb}
</code>

2.5 putIfAbsent

Associates a value with a key only if the key is not already mapped.

<code>public static void putIfAbsent() {
    String ret = map.putIfAbsent("c", "ccc");
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = null, {a=aaa, b=bbb, c=ccc}
</code>

2.6 remove(key, value)

Removes the entry only if the current value matches the supplied value.

<code>public static void remove() {
    boolean ret = map.remove("a", "aaa");
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = true, {b=bbb}
</code>

2.7 computeIfAbsent

Computes and inserts a value for a missing key using the provided mapping function.

<code>public static void computeIfAbsent() {
    String ret = map.computeIfAbsent("c", key -> "ccc");
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = ccc, {a=aaa, b=bbb, c=ccc}
</code>

2.8 computeIfPresent

Updates the value of an existing key; returning null removes the entry.

<code>public static void computeIfPresent() {
    String ret = map.computeIfPresent("a", (k, v) -> "aaa666");
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = aaa666, {a=aaa666, b=bbb}

public static void computeIfPresentRemove() {
    String ret = map.computeIfPresent("a", (k, v) -> null);
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = null, {b=bbb}
</code>

2.9 compute

Computes a new value for a key and replaces it; returning null deletes the key.

<code>public static void compute() {
    String ret = map.compute("a", (k, v) -> "xxxooo");
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = xxxooo, {a=xxxooo, b=bbb}
</code>

2.10 merge

Combines an existing value with a new one using a remapping function.

<code>public static void merge() {
    String ret = map.merge("a", "xxxooo", (oldV, newV) -> oldV + "@" + newV);
    System.out.println("ret = " + ret + ", " + map);
}
// Output: ret = aaa@xxxooo, {a=aaa@xxxooo, b=bbb}
</code>

Java 9 Immutable Factories

of

<code>public static void of() {
    Map<String, String> map = Map.of("a", "aaa", "b", "bbb");
    System.out.println(map);
    // map.remove("a"); // throws UnsupportedOperationException
}
// Output: {a=aaa, b=bbb}
</code>

ofEntries

<code>public static void ofEntries() {
    Map<String, String> map = Map.ofEntries(
        Map.entry("a", "aaa"),
        Map.entry("b", "bbb")
    );
    System.out.println(map);
}
// Output: {a=aaa, b=bbb}
</code>

copyOf

<code>public static void copyOf() {
    Map<String, String> ret = Map.copyOf(map);
    // ret.put("c", "ccc"); // throws UnsupportedOperationException
    System.out.println(ret);
}
</code>

These factory methods create immutable maps; attempts to modify them result in UnsupportedOperationException .

backendJavacollectionsmapJava8
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.