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


Java Map.compute方法代碼示例

本文整理匯總了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);
}
 
開發者ID:thebrightspark,項目名稱:MDC-Discord-Bot,代碼行數:19,代碼來源:AutomodListener.java

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:17,代碼來源:LogbackInstrumentationTest.java

示例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;
}
 
開發者ID:viatsko,項目名稱:hack,代碼行數:18,代碼來源:LongestHarmoniousSubsequence.java

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:19,代碼來源:Log4J2InstrumentationTest.java

示例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;
}
 
開發者ID:viatsko,項目名稱:hack,代碼行數:18,代碼來源:KDiffPairsInAnArray.java

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:19,代碼來源:Log4JInstrumentationTest.java

示例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());

}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:19,代碼來源:Log4JInstrumentationTest.java

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

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:11,代碼來源:LogbackInstrumentationTest.java

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:11,代碼來源:Log4J2InstrumentationTest.java

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:11,代碼來源:Log4J2InstrumentationTest.java

示例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());
}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:11,代碼來源:LogbackInstrumentationTest.java

示例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());

}
 
開發者ID:ApptuitAI,項目名稱:JInsight,代碼行數:33,代碼來源:Log4J2InstrumentationTest.java

示例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();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:PrevisitorTest.java

示例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;
            }
        });
    }
}
 
開發者ID:aws,項目名稱:aws-sdk-java-v2,代碼行數:14,代碼來源:ProfileFile.java


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