如何获取HashMap的Duplicate键值对?

时间:2012-07-24 05:30:16

标签: java hashmap

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

我可以拥有像

这样的值
(1 , 7)
(2 , 4)
(4 , 5)
(3,  7)

不会有任何重复的密钥。只能出现重复值

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

如果我将Duplicate(Key,Value)作为另一个Hashmap,它会很棒。 我该怎么做呢?

我期待输出

 (1 , 7)
 (3,  7)

4 个答案:

答案 0 :(得分:6)

这个怎么样?

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}

答案 1 :(得分: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;
}

答案 2 :(得分: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());
        }
    }

答案 3 :(得分: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忽略我的话......