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