本文整理汇总了Java中java.util.Map.compute方法的典型用法代码示例。如果您正苦于以下问题:Java Map.compute方法的具体用法?Java Map.compute怎么用?Java Map.compute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Map
的用法示例。
在下文中一共展示了Map.compute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkSpamCharacters
import java.util.Map; //导入方法依赖的package包/类
private boolean checkSpamCharacters(String channelName, String content)
{
if(!Config.getContains(EConfigs.CHANNEL_SPAM_CHARACTERS_BLACKLIST, channelName)) return false;
if(content.length() <= 10) return false;
//Count all the characters in the message
Map<Character, Integer> characterCount = new HashMap<>();
for(char c : content.toCharArray())
characterCount.compute(c, (character, integer) -> integer == null ? 1 : integer + 1);
//Add up all of the major repeated characters
int spamCharCount = 0;
for(int charCount : characterCount.values())
if(charCount > (int) Math.ceil((float) content.length() * 0.2f))
spamCharCount += charCount;
return spamCharCount > (int) Math.ceil((float) content.length() * 0.6f);
}
示例2: testLogLevel
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogLevel() throws Exception {
logger.setLevel(Level.ERROR);
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("error", (s, aLong) -> aLong + 1);
logger.trace("trace!");
logger.debug("debug!");
logger.info("info!");
logger.warn("warn!");
logger.error("error!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例3: findLHS
import java.util.Map; //导入方法依赖的package包/类
public int findLHS(int[] nums) {
int result = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.compute(num, (key, value) -> value == null ? 1 : value + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (map.containsKey(entry.getKey() + 1)) {
result = Math.max(result, entry.getValue() + map.get(entry.getKey() + 1));
}
}
return result;
}
示例4: testLogLevel
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogLevel() throws Exception {
setLogLevel(logger, Level.ERROR);
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 2);
expectedCounts.compute("error", (s, aLong) -> aLong + 1);
expectedCounts.compute("fatal", (s, aLong) -> aLong + 1);
logger.trace("trace!");
logger.debug("debug!");
logger.info("info!");
logger.warn("warn!");
logger.error("error!");
logger.fatal("fatal!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例5: findPairs
import java.util.Map; //导入方法依赖的package包/类
public int findPairs(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.compute(num, (key, value) -> value == null ? 1 : value + 1);
}
int result = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if ((k > 0 && map.get(entry.getKey() + k) != null) || (k == 0 && entry.getValue() >= 2)) {
result++;
}
}
return result;
}
示例6: testLogLevel
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogLevel() throws Exception {
logger.setLevel(Level.ERROR);
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 2);
expectedCounts.compute("error", (s, aLong) -> aLong + 1);
expectedCounts.compute("fatal", (s, aLong) -> aLong + 1);
logger.trace("trace!");
logger.debug("debug!");
logger.info("info!");
logger.warn("warn!");
logger.error("error!");
logger.fatal("fatal!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例7: testLoggerReconfiguration
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLoggerReconfiguration() throws Exception {
logger.setLevel(Level.ERROR);
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("error", (s, aLong) -> aLong + 1);
Properties properties = new Properties();
properties.setProperty("log4j.rootCategory", "INFO,TestLog");
properties.setProperty("log4j.appender.TestLog", "org.apache.log4j.ConsoleAppender");
properties.setProperty("log4j.appender.TestLog.layout", "org.apache.log4j.PatternLayout");
PropertyConfigurator.configure(properties);
logger.error("error!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例8: generateTestJar
import java.util.Map; //导入方法依赖的package包/类
public static Path generateTestJar(Pair<String, byte[]>... files) {
Path jarFile;
try {
jarFile = Files.createTempFile("testjar", ".jar");
try(ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(jarFile))) {
Map<String, List<Pair<String, byte[]>>> filesMap = new HashMap<>();
for (Pair<String, byte[]> file : files) {
filesMap.compute(file.getKey(), (k, data) -> {
List<Pair<String, byte[]>> fileData = data != null ? data : new ArrayList<>();
fileData.add(new Pair<>(file.getKey(), compress(file.getValue())));
return fileData;
});
}
filesMap.forEach((path, data) -> {
putPath(Paths.get(path).getParent().toString(), zos);
data.forEach(file -> putFile(file.getKey(), file.getValue(), zos));
});
zos.closeEntry();
zos.finish();
}
} catch (Throwable e) {
SneakyThrow.throwException(e);
return null;
}
//jarFile.toFile().deleteOnExit();
return jarFile;
}
示例9: testLogTrace
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogTrace() throws Exception {
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("trace", (s, aLong) -> aLong + 1);
logger.trace("TRACE!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例10: testLogWarn
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogWarn() throws Exception {
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("warn", (s, aLong) -> aLong + 1);
logger.warn("WARN!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例11: testLogInfo
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogInfo() throws Exception {
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("info", (s, aLong) -> aLong + 1);
logger.info("INFO!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例12: testLogError
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLogError() throws Exception {
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("error", (s, aLong) -> aLong + 1);
logger.error("ERROR!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例13: testLoggerReconfiguration
import java.util.Map; //导入方法依赖的package包/类
@Test
public void testLoggerReconfiguration() throws Exception {
setLogLevel(logger, Level.ERROR);
Map<String, Long> expectedCounts = getCurrentCounts();
expectedCounts.compute("total", (s, aLong) -> aLong + 1);
expectedCounts.compute("error", (s, aLong) -> aLong + 1);
Properties properties = new Properties();
properties.setProperty("name", "PropertiesConfig");
properties.setProperty("appenders", "console");
properties.setProperty("appender.console.type", "Console");
properties.setProperty("appender.console.name", "STDOUT");
properties.setProperty("rootLogger.level", "debug");
properties.setProperty("rootLogger.appenderRefs", "stdout");
properties.setProperty("rootLogger.appenderRefs", "stdout");
properties.setProperty("rootLogger.appenderRef.stdout.ref", "STDOUT");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
properties.store(baos, null);
ConfigurationSource source = new ConfigurationSource(
new ByteArrayInputStream(baos.toByteArray()));
PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
LoggerContext context = (LoggerContext) LogManager.getContext(false);
PropertiesConfiguration configuration = factory.getConfiguration(context, source);
configuration.start();
context.updateLoggers(configuration);
logger.error("error!");
assertEquals(expectedCounts, getCurrentCounts());
}
示例14: transform
import java.util.Map; //导入方法依赖的package包/类
@Override
public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {
if (!isPrevisitCalled) {
throw new AssertionError("Previsit was not called");
}
CustomStringTable table = (CustomStringTable)((ResourcePoolImpl)inResources).getStringTable();
if (table.size() == 0) {
throw new AssertionError("Table is empty");
}
Map<String, Integer> count = new HashMap<>();
for (int i = 0; i < table.size(); ++i) {
String s = table.getString(i);
Optional<ResourcePoolEntry> e = inResources.findEntry(s);
if (e.isPresent()) {
throw new AssertionError();
}
count.compute(s, (k, c) -> 1 + (c == null ? 0 : c));
}
count.forEach((k, v) -> {
if (v != 1) {
throw new AssertionError("Expected one entry in the table, got: " + v + " for " + k);
}
});
inResources.entries().forEach(r -> {
outResources.add(r);
});
return outResources.build();
}
示例15: addToAggregate
import java.util.Map; //导入方法依赖的package包/类
private void addToAggregate(Map<String, Map<String, String>> aggregateRawProfiles, ProfileFile file) {
Map<String, Profile> profiles = file.profiles();
for (Map.Entry<String, Profile> profile : profiles.entrySet()) {
aggregateRawProfiles.compute(profile.getKey(), (k, current) -> {
if (current == null) {
return new HashMap<>(profile.getValue().properties());
} else {
current.putAll(profile.getValue().properties());
return current;
}
});
}
}