Java Code Examples for java.util.Map.replaceAll()

The following are Jave code examples for showing how to use replaceAll() of the java.util.Map class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: Defaults.java   View Source Code Vote up 7 votes
@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());
}
 
Example 2
Project: openjdk-jdk10   File: Defaults.java   View Source Code Vote up 6 votes
@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());
}
 
Example 3
Project: openjdk-systemtest   File: ClassLibraryLambdaTests.java   View Source Code Vote up 5 votes
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++;
	}
}
 
Example 4
Project: hekate   File: Gossip.java   View Source Code Vote up 5 votes
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;
}
 
Example 5
Project: cakes   File: MapNewFuturesDemo.java   View Source Code Vote up 5 votes
/**
 * 新增
 * 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);
}