如何获取HashMap中的重复键值对?

3
HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>();

我可以有像这样的值:

(1 , 7)
(2 , 4)
(4 , 5)
(3,  7)

不会出现重复的键。只会出现重复的值。

我想选择具有重复值的(Key,Value)对。

如果我能得到另一个HashMap作为重复的(Key,Value)对,那就太好了。我该怎么做?

我期望的输出:

 (1 , 7)
 (3,  7)

没听清楚,请您能否举个例子说明一下? - jmj
@Meenakshi:从上面的例子中,您想要(1,7)和(3,7)作为重复值吗? - Syam
由于 (3, 7) 具有重复值 7,他希望将此对 (3, 7) 存储在另一个哈希映射中。我认为是这样。 - Pramod Kumar
4个回答

7
这个怎么样?
public HashMap getDuplicateValues(HashMap in)
{
   // Clone input HashMap because we're removing stuff from it
   in = (HashMap)in.clone();
   HashMap rval = new HashMap();
   Object[] keys = in.keySet().toArray();

   // iterate through all keys
   for(int x=0;x<keys.length;x++) {
      Object value = in.get(keys[x]);
      in.remove(keys[x]);
      // if value is in input HashMap, store it in duplicate HashMap because it has another value
      if(in.containsValue(value)) {
         rval.put(keys[x],value);
      }
      // if value is in duplicate HashMap, store it also because it HAD another value earlier
      if(rval.containsValue(value)) {
         rval.put(keys[x],value);
      }
   }

   return(rval);
}

这个方法将返回输入HashMap中所有重复值的键/值对。


测试代码:

  HashMap map = new HashMap();

  map.put("1","2");
  map.put("2","1");
  map.put("3","8");
  map.put("4","4");
  map.put("5","6");
  map.put("6","8");
  map.put("7","3");
  map.put("8","4");
  map.put("9","4");

  HashMap dups = getDuplicateValues(map);

  System.out.println("MAP = "+map);
  System.out.println("DUP = "+dups);

输出:

MAP = {3=8, 2=1, 1=2, 7=3, 6=8, 5=6, 4=4, 9=4, 8=4}
DUP = {3=8, 6=8, 4=4, 9=4, 8=4}

@Meenakshi 试试这段代码,它是有效的。 - Azuu

1

您不能有重复的键。可以将其想象成一堆盒子,每个盒子里只能放置一件物品。您可以将锤子放在第1个盒子中,将键盘放在第2个盒子中,将手电筒放在第3个盒子中,将另一个锤子放在第4个盒子中。但是,您不能将两个锤子或一个锤子和一个键盘放在第1个盒子中,因为它只有容纳单个物品的空间。如果您尝试向已经装满的盒子中添加另一件物品,它将自动将其取出并丢弃旧物品。那么就无法再访问它了。

不过,我认为我可能误解了这个问题;您能更好地解释一下您要检索/执行的确切内容吗?

好的,这里有一些代码,可以基本上反转您的HashMap:

public static void main(String[] args) throws ParseException {
    HashMap start = new HashMap();
    start.put(1, 7);
    start.put(2, 4);
    start.put(4, 5);
    start.put(3, 7);

    HashMap<Object, ArrayList<Object>>  reversed = reverse(start);

    //Some code to print out our results
    Set<Entry<Object, ArrayList<Object>>> set = reversed.entrySet();

    for(Entry entry : set) {
        System.out.println(entry.getKey() + ": " + entry.getValue());
        //if we want here, we can check if the size of the value (The 
    //ArrayList of old keys who has a value of this guy's key) is over 1, if so,
    //there were duplicates of some value (stored to this entry's key)
    }
}
public static HashMap<Object, ArrayList<Object>> reverse(HashMap map) {
    HashMap<Object, ArrayList<Object>> newMap = 
            new HashMap<Object, ArrayList<Object>>();

    Set<Entry> set = map.entrySet();
    for(Entry entry : set) {
        ArrayList list = new ArrayList();
        if(newMap.containsKey(entry.getValue())) {
            list=newMap.get(entry.getValue());
        }
        list.add(entry.getKey());
        newMap.put(entry.getValue(), list);
    }
    return newMap;
}

我刚刚发表了一份声明,以便明确表明,当我说重复时,指的是值而不是键。 - Meenakshi

1
    HashMap<Integer, Integer> sample = new HashMap<Integer, Integer>();
    Integer valueForSearch = 7;
    HashMap<Integer, Integer> result = new HashMap<Integer, Integer>();
    for (Entry<Integer, Integer> entry : sample.entrySet()) {
        if (entry.getValue().equals(valueForSearch)) {
            result.put(entry.getKey(), entry.getValue());
        }
    }

这不是通用代码。它只会找出值是否为7。 - Pramod Kumar
@Jin35 我可能不知道7只出现了两次。可能会有5个数出现3次,或者可能还有其他的数出现多次。 - Meenakshi
没错,但如果你想获取所有的重复值,结果将会是 HashMap<value, HashMap<key,value>> - Jin35

0

仅概述一下......

  Object array[] = hashmapsample.keySet().toArray();
  for(int i=0;i<array.length();i++)
  {
         if(hashmapsample.containsValue(hashmapsample.get(array[i]){
    //Put that particular value in another hashmap here
    }     
  }

啊..我发现这个帖子里有很多详细的答案 :D 那就忽略我的吧..

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接