JAVA8 map操作 &删除元素 实战

MAP例子代码如下:

// 根据map中得值去判断删除, 这里value就是map的值对象
map.values().removeIf(value -> !value.contains("test"));
// 根据key删除
map.keySet().removeIf(key -> key != 9);
//通过getkey()方法获得值去删除
map.entrySet().removeIf(entry -> entry.getKey() != 9);

Map<Integer, String> map = new HashMap<>();
    map.put(1,"bincai");
    for (int i = 0; i < 3; i++) {
    // 如果这个key不存在,就put进去
      map.putIfAbsent(i, "val" + i);
    }
  
  Map<String, Integer> wordCounts = new ConcurrentHashMap<>(10);
        String s =
                "Lorem ipsum dolor sit amet consetetur iam nonumy sadipscing " +
                        " elitr, sed diam nonumy eirmod tempor invidunt ut erat sed " +
                        "labore et dolore magna dolor sit amet aliquyam erat sed diam";
 
        wordCounts.put("sed", 0);
        wordCounts.put("magna",0);
        for (String t : s.split(" ")) {
            wordCounts.computeIfPresent(t, (k, v) -> v + 1);
        }

你可能感兴趣的:(java,java)