当前位置: 首页>>代码示例>>Java>>正文


Java Map.computeIfPresent方法代码示例

本文整理汇总了Java中java.util.Map.computeIfPresent方法的典型用法代码示例。如果您正苦于以下问题:Java Map.computeIfPresent方法的具体用法?Java Map.computeIfPresent怎么用?Java Map.computeIfPresent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.Map的用法示例。


在下文中一共展示了Map.computeIfPresent方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: removeClient

import java.util.Map; //导入方法依赖的package包/类
/**
 * Decreasing number of active connection from player's IP address
 * @param eventId
 * @param client
 * @return true if success and false if any problem detected.
 */
public final boolean removeClient(int eventId, L2GameClient client)
{
	if (client == null)
	{
		return false; // unable to determine IP address
	}
	
	final Map<Integer, AtomicInteger> event = _eventIPs.get(eventId);
	if (event == null)
	{
		return false; // no such event registered
	}
	
	final Integer addrHash = Integer.valueOf(client.getConnectionAddress().hashCode());
	
	return event.computeIfPresent(addrHash, (k, v) ->
	{
		if ((v == null) || (v.decrementAndGet() == 0))
		{
			return null;
		}
		return v;
	}) != null;
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:31,代码来源:AntiFeedManager.java

示例2: replacePlaceholders

import java.util.Map; //导入方法依赖的package包/类
@Contract("_, null -> fail; null, _ -> null")
private String replacePlaceholders(String source, Map<String, String> replacements) {
    Ensure.notNull(replacements, "Replacements map shouldn't be null!");
    if(source == null) return null;

    /* Find placeholders */
    List<String> foundPlaceholders = new ArrayList<>();
    StringBuilder lastPlaceholder = null;
    for(char c: source.toCharArray()) {
        if(c == '{' && lastPlaceholder == null) {
            lastPlaceholder = new StringBuilder();
        } else if(lastPlaceholder != null && c != '}') {
            lastPlaceholder.append(c);
        } else if(lastPlaceholder != null) {
            foundPlaceholders.add(lastPlaceholder.toString());
            lastPlaceholder = null;
        }
    }

    /* Amazing hack, wow */
    String[] a = new String[] { source };

    /* Replace placeholders */
    for(String placeholder: foundPlaceholders) {
        replacements.computeIfPresent(placeholder, (k, value) -> {
            a[0] = a[0].replace("{" + k + "}", value);
            return value;
        });
    }

    return a[0];
}
 
开发者ID:mikroskeem,项目名称:Shuriken,代码行数:33,代码来源:MethodReflectorFactory.java

示例3: testComputeIfPresent

import java.util.Map; //导入方法依赖的package包/类
private static <T> void testComputeIfPresent(Map<T, T> map, String desc, T[] keys,
        BiFunction<T, T, T> mappingFunction) {
    // remove a third of the keys
    // call testComputeIfPresent for all keys[]
    // removed keys should remain absent, even keys should be mapped to $RESULT
    // no value from keys[] should be in map
    T funcResult = mappingFunction.apply(keys[0], keys[0]);
    int expectedSize1 = 0;
    removeThirdKeys(map, keys);

    for (int i = 0; i < keys.length; i++) {
        T retVal = map.computeIfPresent(keys[i], mappingFunction);
        if (i % 3 != 2) { // key present
            if (funcResult == null) { // was removed
                assertFalse(map.containsKey(keys[i]),
                        String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
            } else { // value was replaced
                assertTrue(map.containsKey(keys[i]),
                        String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
                expectedSize1++;
            }
            assertEquals(retVal, funcResult,
                    String.format("computeIfPresent: retVal(%s[%s])", desc, i));
            assertEquals(funcResult, map.get(keys[i]),
                    String.format("replaceIfMapped: get(%s[%d])", desc, i));

        } else { // odd: was removed, should not be replaced
            assertNull(retVal,
                    String.format("replaceIfMapped: retVal(%s[%d])", desc, i));
            assertNull(map.get(keys[i]),
                    String.format("replaceIfMapped: get(%s[%d])", desc, i));
            assertFalse(map.containsKey(keys[i]),
                    String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
        }
        assertFalse(map.containsValue(keys[i]),
                String.format("replaceIfMapped: !containsValue(%s[%d])", desc, i));
    }
    assertEquals(map.size(), expectedSize1,
            String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:41,代码来源:InPlaceOpsCollisions.java


注:本文中的java.util.Map.computeIfPresent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。