天天看點

【Java 8 新特性】Java Map replaceAll() 示例 | 根據函數替換Map中所有條目的值參考文獻

Java Map replaceAll 示例 | 根據函數替換Map中所有條目的值

  • 參考文獻

replaceAll

java.util.Map

的預設方法,在

Java 8

中被引入。

replaceAll

方法接受

BiFunction

作為一個參數。

replaceAll

方法将每個條目的值替換為對該條目調用指定函數的結果。

replaceAll

方法對

Map

的每個條目都起作用,如果指定的函數對任何條目都産生異常,則該方法停止。

Java

文檔中找到該方法的聲明。

我們需要傳遞

BiFunction

,它将适用于

Map

的每個條目。

例1:在本例中,我們使用

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);     
  }
} 
           

輸出

--- before replaceAll() ---
{101=Mahesh, 102=Suresh, 103=Krishna}
--- after replaceAll() ---
{101=Mahesh-101, 102=Suresh-102, 103=Krishna-103} 
           

同樣可以通過使用

Map.Entry

疊代

Map

來實作。

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);     
  }
} 
           

輸出

--- before replaceAll() ---
{101=Mahesh, 102=Suresh, 103=Krishna}
--- after replaceAll() ---
{101=Mahesh-101, 102=Suresh-102, 103=Krishna-103} 
           

例2:再找一個使用

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);     
  }
} 
           

輸出

--- before replaceAll() ---
{101=Mahesh, 102=Suresh, 103=Krishna}
--- after replaceAll() ---
{101=Mahesh, 102=Suresh-102, 103=Krishna} 
           

例3:在本例中,我們使用的是

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);
  }
} 
           

輸出

--- before replaceAll() ---
{1=100, 2=200, 3=300}
--- after replaceAll() ---
{1=100, 2=400, 3=900} 
           

例4:在本例中,我們使用

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);
  }
} 
           

輸出

--- before replaceAll() ---
{Bharat=Modi, Russia=Putin, USA=Trump}
--- after replaceAll() ---
{Bharat=Mr. Modi, Russia=Mr. Putin, USA=Mr. Trump} 
           

參考文獻

【1】Java doc: Map

【2】Java Map replaceAll() Example