在Java Map中查找重复值?

时间:2011-08-01 07:10:40

标签: java map hashmap

我想在HashMap中显示值。 HashMap可能有重复值(但不是重复键),但我想只显示一次值。

所以我应该找到Map是否有重复的值。我知道我们可以迭代Map并使用map.containsValue(value)的返回布尔值。我想知道是否有任何方法可以在map中找到重复值,或者我自己应该编写代码?

7 个答案:

答案 0 :(得分:19)

一个简单的解决方案是将值列表的大小与您设置的值进行比较。

// pseudo-code
List<T> valuesList = map.values();
Set<T> valuesSet = new HashSet<T>(map.values);
// check size of both collections; if unequal, you have duplicates

答案 1 :(得分:6)

示例:

Map<Object, Object> map = new HashMap<Object, Object>();
map.put(1,2);
map.put(3,4);
map.put(2,2);
map.put(5,3);

Set<Object> uniqueValues = new HashSet<Object>(map.values());

System.out.println(uniqueValues);

输出:

[2, 3, 4]

答案 2 :(得分:1)

没有提供jdk1.6这样的方法。

您可以采取的一种简单方法是

  • 从列表中的地图中获取所有值
  • 将该列表放入一个将删除重复项的集合

答案 3 :(得分:1)

使用apache commons库类的方法

org.apache.commons.collections.MapUtils.invertMap(map)

并比较实际地图的大小和反转地图。

答案 4 :(得分:0)

试试这段代码

private boolean hasDuplicates(Map<Integer, List<String>> datamap){
boolean status = false;


    Set valueset=new HashSet(datamap.values());

    if(datamap.values().size()!=valueset.size()){
    status=true;
    }
    else{
    status = false;
    }


    return status;

}

答案 5 :(得分:0)

try this code but this is not optimize code :

public class HashMapDulicate {
    public static void main(String[] args) {        
        Map<String,Integer> map=new HashMap<>();
        map.put("A", 1);
        map.put("B", 1);
        map.put("C", 3);
        map.put("D", 4);


        Set set=new HashSet<>();
        List list=new ArrayList<>();

        for(Entry<String, Integer> mapVal:map.entrySet()) {

            if(!set.add(mapVal.getValue())) {
                list.add(mapVal.getValue());

            }else {
                set.add(mapVal.getValue());
            }

        }

for(Entry<String, Integer> mapVal:map.entrySet()) {

    if(list.contains(mapVal.getValue())){

        System.out.println(mapVal.getKey() +":" + mapVal.getValue());
    }
}
    }
}

答案 6 :(得分:0)

public static void main(String[] args) {

        HashMap<String, Integer> map = new HashMap<>();
        map.put("abc", 2);
        map.put("def", 1);
        map.put("hij", 4);
        map.put("klm", 6);
        map.put("nop", 2);
        map.put("qrs", 2);
        map.put("tuv", 6);
        map.put("wxy", 8);
        map.put("zab", 1);
        map.put("cde", 5);
        map.put("fgh", 4);
        map.put("ijk", 3);

        HashMap<Integer, String> duplicatMap = new HashMap<>();

        Set<Entry<String, Integer>> entrySet = map.entrySet();
        Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
        while(iterator.hasNext()) {
            Entry<String, Integer> entry = iterator.next();
            String key = entry.getKey();
            Integer value = entry.getValue();

            if(duplicatMap.containsKey(value)) {
                duplicatMap.put(value, duplicatMap.get(value)+", "+key);
            } else {
                duplicatMap.put(value, key);
            }
        }
        System.out.println(duplicatMap);

    } 

outPut:-{1 = def,zab,2 = abc,qrs,nop,3 = ijk,4 = fgh,hij,5 = cde,6 = tuv,klm,8 = wxy}     如果要修改,请再次使用EntrySet。