To modify a HashMap in Java, you can use the following built-in methods

  • Use put(key, value) or putIfAbsent(key, value) to add key-value pairs/mappings

  • Use compute(key, BiFunction), computeIfAbsent(key, BiFunction), replace(key, value), or replace(key, oldValue, newValue) methods to update values

  • Use remove(key) methods to remove key-value pair entries

Let's walk through this tutorial to explore in more details

What you'll need

  • JDK 8+
  • Your favorite IDE

Tech stack

  • Java 8+
  • JUnit for writing test cases
  • AssertJ for verifying the test result

Add key-value mapping pairs into a HashMap

  • Use the put(key, value) method to add the key-value pair mapping to a map. If the specified key has existed, the old value will be replaced by the specified value
@Test
public void put() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", 2);
    assertThat(map.get("k2")).isEqualTo(2);

    map.put("k2", 20);
    assertThat(map.get("k2")).isEqualTo(20);
}
  • Use Java 8+ putIfAbsent(key, value) method to add the key-value pair mapping to a map. If the specified key has existed, the old value will not be replaced by the specified value if it is not null
@Test
public void putIfAbsent() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", 2);
    map.put("k3", null);

    map.putIfAbsent("k4", 4);
    assertThat(map.get("k4")).isEqualTo(4);

    map.putIfAbsent("k2", 20);
    assertThat(map.get("k2")).isEqualTo(2);

    map.putIfAbsent("k3", 3);
    assertThat(map.get("k3")).isEqualTo(3);
}
  • Use putAll(Map) to copy and add all key-value pair mappings from the specified map to the caller map
@Test
public void putAll() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    Map<String, Integer> map2 = new HashMap<>();
    map2.put("k3", 3);
    map2.putAll(map);

    assertThat(map2.size()).isEqualTo(3);
}

Update values in HashMap

  • Use Java 8+ compute(key, BiFunction) method to replace the old value with the value returned by the specified BiFunction if the specified key has existed in the map

    The following example increases HashMap value by using compute

@Test
public void increaseValueWithCompute() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    map.compute("k2", (k,v) -> (v == null ? 0 : v) + 1);
    assertThat(map.get("k2")).isEqualTo(1);
}
  • Use Java 8+ replace(key, value) method to replace the old value with the specified value if the specified key has existed in the map
@Test
public void replace() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    map.replace("k2", 2);
    assertThat(map.get("k2")).isEqualTo(2);

    map.replace("k3", 3);
    assertThat(map.get("k3")).isNull();
}
  • Use Java 8+ replace(key, oldValue, newValue) method to replace the specified oldValue with the specified newValue if the specified key has associated with oldValue in the map
@Test
public void replaceIfAssociated() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    map.replace("k1", 1, 10);
    assertThat(map.get("k1")).isEqualTo(10);

    map.replace("k2", 2, 20);
    assertThat(map.get("k2")).isNull();
}
  • Use Java 8+ replaceAll(BiFunction) method to replace every value in the map with the value returned by the specified BiFunction
@Test
public void replaceAll() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    map.replaceAll((k, v) -> 100);
    assertThat(map.get("k1")).isEqualTo(100);
    assertThat(map.get("k2")).isEqualTo(100);
}

Remove HashMap entries

  • Use remove(key) method to remove the key-value mapping by the specified key
@Test
public void removeByKey() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    Integer v = map.remove("k1");
    assertThat(v).isEqualTo(1);
    assertThat(map).hasSize(1);

    v = map.remove("k3");
    assertThat(v).isNull();
    assertThat(map).hasSize(1);
}
  • Use Java 8+ remove(key, value) method to remove the key-value mapping by the specified key and value
@Test
public void removeByKeyValue() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    boolean r = map.remove("k1", 10);
    assertThat(r).isFalse();
    assertThat(map).hasSize(2);

    r = map.remove("k2", null);
    assertThat(r).isTrue();
    assertThat(map).hasSize(1);

    r = map.remove("k3", 3);
    assertThat(r).isFalse();
    assertThat(map).hasSize(1);
}
  • Use clear() method removes all key-value mapping entries from the map
@Test
public void clear() {  
    Map<String, Integer> map = new HashMap<>();
    map.put("k1", 1);
    map.put("k2", null);

    map.clear();
    assertThat(map).hasSize(0);
}

Conclusion

In this tutorial, we learn to add, update and remove the key-value mapping from a HashMap. You can find the full source code as below


Share to social

Van N.

Van N. is a software engineer, creator of HelloKoding. He loves coding, blogging, and traveling. You may find him on GitHub and LinkedIn