本文整理匯總了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;
}
示例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];
}
示例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));
}