replaceAll
是 java.util.Map
的默认方法,已在 Java 8 中引入。replaceAll
方法接受 BiFunction
作为参数。 replaceAll
方法将每个条目值Replace为对该条目调用给定函数的结果。 replaceAll
适用于 Map
的每个条目,或者如果指定函数对任何条目抛出异常,它就会停止。从 Java 文档中找到方法声明。
default void replaceAll(BiFunction<? super K,? super V,? extends V> function)
我们需要传递将应用于 Map
的每个条目的 BiFunction
。
在此示例中,我们使用的是 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}
再找一个使用 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}
在此示例中,我们使用的是 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}
在此示例中,我们使用的是 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}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.concretepage.com/java/java-8/java-map-replaceall
内容来源于网络,如有侵权,请联系作者删除!