當前位置: 首頁>>代碼示例>>Java>>正文


Java Map.replaceAll方法代碼示例

本文整理匯總了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());
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:21,代碼來源:Defaults.java

示例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++;
	}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:9,代碼來源:ClassLibraryLambdaTests.java

示例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;
}
 
開發者ID:hekate-io,項目名稱:hekate,代碼行數:30,代碼來源:Gossip.java

示例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);
}
 
開發者ID:cbooy,項目名稱:cakes,代碼行數:38,代碼來源:MapNewFuturesDemo.java


注:本文中的java.util.Map.replaceAll方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。