How to Remove Items/Entries with Specific Values from Map/HashMa

  • 时间:2020-09-07 12:13:31
  • 分类:网络文摘
  • 阅读:124 次
2091E060-120E-4C44-BAA3-7E4E0DF7BD55 How to Remove Items/Entries with Specific Values from Map/HashMap Object in Java? java programming languages

Java

In Java, you can usually remove items or entries with some specific value from Map or HashMap object using traditional method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Map<String, String> removeItemByValue(Map<String, String> data, String value) {
  for (var key: data.keySet()) {
    if (data.get(key) == null) {
       data.remove(key);
    }
  }
  return data;
}
 
var data = new HashMap<String, String>();
data.put("a", "1");
data.put("b", "2");
data.put("c", "2");
removeItemByValue(data, null); 
// only prints a = 1
for (var key: data.keySet()) {
  System.out.println(key + "=" + data.get(key)); 
}
public Map<String, String> removeItemByValue(Map<String, String> data, String value) {
  for (var key: data.keySet()) {
    if (data.get(key) == null) {
       data.remove(key);
    }
  }
  return data;
}

var data = new HashMap<String, String>();
data.put("a", "1");
data.put("b", "2");
data.put("c", "2");
removeItemByValue(data, null); 
// only prints a = 1
for (var key: data.keySet()) {
  System.out.println(key + "=" + data.get(key)); 
}

A Better Map-Item Removal Approach via using values()

The values() method in Map object actually returns a Collection view of the valujes that are contained in the map object. And the remove object return null when the item is not existent anymore. Thefore, we can use the following modern 1-liner in Java to remove the items in a Map that have the specific value.

1
2
3
4
public Map<String, String> removeItemByValue(Map<String, String> data, String value) {
  while (data.values().remove(value)) ;
  return data;
}
public Map<String, String> removeItemByValue(Map<String, String> data, String value) {
  while (data.values().remove(value)) ;
  return data;
}

To double check and verify that items are actually removed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.helloacm;
 
import java.util.HashMap;
import java.util.Map;
 
public class Main {
    public static void main(String[] args) {
        var data = new HashMap<String, String>();
        data.put("a", "b");
        data.put("e", "2");
        data.put("f", "2");
        while (data.values().remove("2"));
        // prints "a"        
        for (Map.Entry<String, String> entry: data.entrySet()) {
            System.out.println(entry.getKey());
        }
    }
}
package com.helloacm;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        var data = new HashMap<String, String>();
        data.put("a", "b");
        data.put("e", "2");
        data.put("f", "2");
        while (data.values().remove("2"));
        // prints "a"        
        for (Map.Entry<String, String> entry: data.entrySet()) {
            System.out.println(entry.getKey());
        }
    }
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
价值作文650字  我的菜园作文  数学题:如果从甲队调出30人到乙队  奥数题:小军骑车从家里出发到某地参观  数学题:现在有牛,羊,马吃一块草地的草,草均匀的生长  数学题:利用图中的阴影部分刚好可以做成一个圆柱形油桶  数学题:水面高度正好是圆锥高度的一半  数学题:把高是10cm的圆柱按下图切开,分成17份  数学题:一批同学画画,2人合用一瓶红颜料  数学题:再加多少毫升牛奶,才可以把杯子装满? 
评论列表
添加评论