本文整理汇总了Java中java.util.Map.replaceAll方法的典型用法代码示例。如果您正苦于以下问题:Java Map.replaceAll方法的具体用法?Java Map.replaceAll怎么用?Java Map.replaceAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Map
的用法示例。
在下文中一共展示了Map.replaceAll方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testReplaceAll
import java.util.Map; //导入方法依赖的package包/类
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public static void testReplaceAll(String description, Map<IntegerEnum, String> map) {
IntegerEnum[] EACH_KEY = new IntegerEnum[map.size()];
Set<String> EACH_REPLACE = new HashSet<>(map.size());
map.replaceAll((k,v) -> {
int idx = (null == k) ? 0 : k.ordinal(); // substitute for index.
assertNull(EACH_KEY[idx]);
EACH_KEY[idx] = (idx == 0) ? KEYS[0] : k; // substitute for comparison.
assertSame(v, map.get(k));
String replacement = v + " replaced";
EACH_REPLACE.add(replacement);
return replacement;
});
assertEquals(KEYS, EACH_KEY, description);
assertEquals(map.values().size(), EACH_REPLACE.size(), description + EACH_REPLACE);
assertTrue(EACH_REPLACE.containsAll(map.values()), description + " : " + EACH_REPLACE + " != " + map.values());
assertTrue(map.values().containsAll(EACH_REPLACE), description + " : " + EACH_REPLACE + " != " + map.values());
}
示例2: runReplaceAllMap
import java.util.Map; //导入方法依赖的package包/类
void runReplaceAllMap(Map<Integer,StringHolder> a, Map<Integer,StringHolder> b) {
a.replaceAll((Integer f, StringHolder t)->{t.concat("@JJ17r");return new StringHolder(t.getString() + "@417b");});
int x = 0;
while (x<b.size()) {
assertTrue("Expected \"" + b.get(x).getString() + "[email protected]@417b\" but found \"" + a.get(x).getString(),a.get(x).getString().equals(b.get(x).getString() + "[email protected]@417b"));
x++;
}
}
示例3: suspectedView
import java.util.Map; //导入方法依赖的package包/类
public SuspectedNodesView suspectedView() {
Map<ClusterNodeId, Set<ClusterNodeId>> suspected = null;
for (GossipNodeState s : members.values()) {
Set<ClusterNodeId> nodeSuspected = s.suspected();
if (!nodeSuspected.isEmpty()) {
if (suspected == null) {
suspected = new HashMap<>();
}
for (ClusterNodeId id : nodeSuspected) {
Set<ClusterNodeId> suspectedBy = suspected.computeIfAbsent(id, k -> new HashSet<>());
suspectedBy.add(s.id());
}
}
}
if (suspected != null) {
suspected.replaceAll((id, set) -> unmodifiableSet(set));
suspected = unmodifiableMap(suspected);
return new SuspectedNodesView(suspected);
}
return SuspectedNodesView.EMPTY;
}
示例4: testReplace
import java.util.Map; //导入方法依赖的package包/类
/**
* 新增
* replace(key,value)
* replace(key,oldValue,newValue)
* replaceAll(BiFunction fun)
*/
@Test
public void testReplace() {
Map<String, String> hashMap = Maps.newHashMap();
hashMap.put("k1", "v1");
hashMap.put("k3", "v3");
hashMap.put("k6", "v6");
// replace 替换指定key的value
String replaceValue = hashMap.replace("k6", "haha6");
System.out.println("map replace key k6 then map=" + hashMap + ", and the return value=" + replaceValue);
System.out.println("\n-------------------------------------------\n");
// replace 替换指定key和oldValue的数据,如果key和oldValue对应不上则替换失败
boolean isReplaceOldValue = hashMap.replace("k6", "what?", "heng");
System.out.println("replace map by key and oldValue first try isReplaceOldValue=" + isReplaceOldValue);
boolean isReplaceOldValue2 = hashMap.replace("k6", "haha6", "heng");
System.out.println("replace map by key and oldValue second try isReplaceOldValue2=" + isReplaceOldValue2);
System.out.println("\n-------------------------------------------\n");
// replaceAll, 根据给定的key和value计算要替换的数据
hashMap.replaceAll((k, v) -> {
if (k.equals("k3") && v.equals("v3")) {
return "hahahahaha";
}
return v;
});
System.out.println("biMap replaceAll = " + hashMap);
}