Java Map replaceAll() Example

By Arvind Rai, February 14, 2020
The replaceAll is the default method of java.util.Map and has been introduced in Java 8. The replaceAll method accepts BiFunction as an argument. The replaceAll method replaces each entry value with the result of invoking given function on that entry. The replaceAll works for each entry of the Map or it stops if specified function throws exception for any entry. Find the method declaration from Java doc.
default void replaceAll(BiFunction<? super K,? super V,? extends V> function) 
We need to pass BiFunction that will apply to each entry of the Map.

Example-1

In this example, we are using HashMap.
ReplaceAll1.java
import java.util.HashMap;
import java.util.Map;

public class ReplaceAll1 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     studentMap.put(103, "Krishna");
     
     System.out.println("--- before replaceAll() ---");
     
     System.out.println(studentMap);
     
     studentMap.replaceAll((k,v) -> v + "-" + k);
     
     System.out.println("--- after replaceAll() ---");
     
     System.out.println(studentMap);     
  }
} 
Output
--- before replaceAll() ---
{101=Mahesh, 102=Suresh, 103=Krishna}
--- after replaceAll() ---
{101=Mahesh-101, 102=Suresh-102, 103=Krishna-103} 
The same can be achieved by iterating Map with Map.Entry.
MapEntryTest.java
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class MapEntryTest {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     studentMap.put(103, "Krishna");
     
     System.out.println("--- before replaceAll() ---");
     
     System.out.println(studentMap);
     
     BiFunction<Integer, String, String> function = (k, v) -> v + "-" + k;
     
     for (Map.Entry<Integer, String> entry : studentMap.entrySet())
       entry.setValue(function.apply(entry.getKey(), entry.getValue()));
     
     System.out.println("--- after replaceAll() ---");
     
     System.out.println(studentMap);     
  }
} 
Output
--- before replaceAll() ---
{101=Mahesh, 102=Suresh, 103=Krishna}
--- after replaceAll() ---
{101=Mahesh-101, 102=Suresh-102, 103=Krishna-103} 

Example-2

Find one more example using HashMap.
ReplaceAll2.java
import java.util.HashMap;
import java.util.Map;

public class ReplaceAll2 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     studentMap.put(103, "Krishna");
     
     System.out.println("--- before replaceAll() ---");
     
     System.out.println(studentMap);
     
     studentMap.replaceAll((k,v) -> {
       if (k == 102) {
         return v + "-" + k;
       }
       return v;
     });
     
     System.out.println("--- after replaceAll() ---");
     
     System.out.println(studentMap);     
  }
} 
Output
--- before replaceAll() ---
{101=Mahesh, 102=Suresh, 103=Krishna}
--- after replaceAll() ---
{101=Mahesh, 102=Suresh-102, 103=Krishna} 

Example-3

In this example, we are using LinkedHashMap.
ReplaceAll3.java
import java.util.LinkedHashMap;
import java.util.Map;

public class ReplaceAll3 {
  public static void main(String[] args) {
     Map<Integer, Integer> numberMap = new LinkedHashMap<>();
     numberMap.put(1, 100);
     numberMap.put(2, 200);
     numberMap.put(3, 300);
     
     System.out.println("--- before replaceAll() ---");
     
     System.out.println(numberMap);
     
     numberMap.replaceAll((k, v) -> v * k);
     
     System.out.println("--- after replaceAll() ---");
     
     System.out.println(numberMap);
  }
} 
Output
--- before replaceAll() ---
{1=100, 2=200, 3=300}
--- after replaceAll() ---
{1=100, 2=400, 3=900} 

Example-4

In this example, we are using TreeMap.
ReplaceAll4.java
import java.util.Map;
import java.util.TreeMap;

public class ReplaceAll4 {
  public static void main(String[] args) {
     Map<String, String> treeMap = new TreeMap<>();
     treeMap.put("Bharat", "Modi");
     treeMap.put("Russia", "Putin");
     treeMap.put("USA", "Trump");
     
     System.out.println("--- before replaceAll() ---");
     System.out.println(treeMap);
     
     treeMap.replaceAll((k, v) -> "Mr. "+ v);
     
     System.out.println("--- after replaceAll() ---");
     System.out.println(treeMap);
  }
} 
Output
--- before replaceAll() ---
{Bharat=Modi, Russia=Putin, USA=Trump}
--- after replaceAll() ---
{Bharat=Mr. Modi, Russia=Mr. Putin, USA=Mr. Trump} 

Reference

Java doc: Map
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us