Using Default Methods of Map from Java 8


Examples of using Default Methods introduced in Java 8 in Map interface

Using getOrDefault:

Returns the value mapped to the key, or if the key is not present, returns the default value

Map<Integer, String> myMap = new HashMap<>();
myMap.put(10, "Primary element");
myMap.get(10); // => Primary element
myMap.get(20); // => null
myMap.getOrDefault(20, "Fallback content"); // => Fallback content
 

Using forEach:

Allows to perform the operation specified in the 'action' on each Map Entry

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
 
map.forEach((key, value) -> System.out.println("Key: "+key+ " :: Value: "+value));
// Key: 1 :: Value: one
// Key: 2 :: Value: two
// Key: 3 :: Value: three

Using replaceAll:

Will replace with new-value only if key is present

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Tiya", 20);
map.put("Kim", 30);
map.put("Siva", 40);
map.replaceAll((key,value)->value+10); //{Tiya=30, Kim=40, Siva=50}

Using putIfAbsent:

Key-Value pair is added to the map, if the key is not present or mapped to null

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Tiya", 20);
map.put("Kim", 30);
map.put("Siva", 40);
map.putIfAbsent("kelly", 50); //{Tiya=20, Kim=30, Siva=40, kelly=50}

Using remove:

Removes the key only if its associated with the given value

 Map<String, Integer> map = new HashMap<String, Integer>();
 map.put("Sathish", 20);
 map.put("Leo", 30);
 map.put("Bob", 40);
 map.remove("Bob",40); //{Sathish=30, Leo=40}

Using replace:

If the key is present then the value is replaced by new-value. If the key is not present, does nothing.

Map<String, Integer> myMap = new HashMap<>();
myMap.put("alice", 25);
myMap.put("bob", 35);
myMap.put("charlie", 45);
myMap.replace("charlie", 55); //{alice=25, bob=35, charlie=55}
myMap.replace("daniel", 65); //{alice=25, bob=35, charlie=55}

Using computeIfAbsent:

This method adds an entry in the Map. the key is specified in the function and the value is the result of the application of the mapping function

import java.util.HashMap;
import java.util.Map;
 
public class DifferentMapExample {
    public static void main(String[] args) {
        Map<String, Integer> scores = new HashMap<>();
        scores.put("alice", 25);
        scores.put("bob", 35);
        scores.put("charlie", 45);
 
        scores.computeIfAbsent("david", k -> scores.get("alice") + 10); // {alice=25, bob=35, charlie=45, david=35}
        scores.computeIfAbsent("bob", k -> scores.get("alice") + 10); // {alice=25, bob=35, charlie=45, david=35}
        // bob is already present
    }
}

Using computeIfPresent:

This method adds an entry or modifies an existing entry in the Map. Does nothing if an entry with that key is not present

Map<String, Integer> scores = new HashMap<>();
scores.put("emma", 25);
scores.put("daniel", 30);
scores.put("lucas", 40);
 
scores.computeIfPresent("kate", (k, v) -> v + 15); // {emma=25, daniel=30, lucas=40} // kate not present
scores.computeIfPresent("lucas", (k, v) -> v + 10); // {emma=25, daniel=30, lucas=50} // lucas present, so increase the value

Using compute:

This method replaces the value of a key by the newly computed value

Map<String, Integer> scores = new HashMap<>();
scores.put("alice", 25);
scores.put("bob", 30);
scores.put("charlie", 40);
 
scores.compute("bob", (k, v) -> v + 50); // {alice=25, bob=80, charlie=40} // Increase the value

Using merge:

Adds the key-value pair to the map, if key is not present or value for the key is null Replaces the value with the newly computed value, if the key is present Key is removed from the map , if new value computed is null

Map<String, Integer> scores = new HashMap<>();
scores.put("emma", 25);
scores.put("finn", 30);
scores.put("grace", 40);
 
// Adds the key-value pair to the map, if key is not present or value for the key is null
scores.merge("hannah", 50, (k, v) -> scores.get("emma") + 10); // {emma=25, finn=30, grace=40, hannah=50}
 
// Replaces the value with the newly computed value if the key is present
scores.merge("finn", 50, (k, v) -> scores.get("emma") + 10); // {emma=25, finn=35, grace=40, hannah=50}
 
// Key is removed from the map if the new value computed is null
scores.merge("grace", 30, (k, v) -> scores.get("noah")); // {emma=25, finn=35, hannah=50}

Basic Programs