用于在HashMap / HashTable </key,value>中防止重复<key,value>对的Java代码

时间:2010-12-11 07:58:09

标签: java

我有一个HashMap如下(假设它有10,0000个元素)

HashMap<String,String> hm = new HashMap<String,String>();
   hm.put("John","1");
   hm.put("Alex","2");
   hm.put("Mike","3");
   hm.put("Justin","4");
   hm.put("Code","5");

==========================
    Expected Output     
==========================  

Key = John",Value = "1"
   Key = Alex",Value = "2"
   Key = Mike",Value = "3"
   Key = Justin",Value = "4"
   Key = Code",Value = "5"
   ===========================

我需要Java代码来防止HashMap中的Addition of Duplicate <Key,Value> Pairs这样    以下条件是满意的   1 GT; hm.put("John","1"); is not accepted/added again in the Map
  2 - ; hm.put("John","2"); is not accepted/added again in the Map

希望清楚。 提供的Java代码将不胜感激。(因为我可以向现有地图添加任何副本,所以需要通用解决方案)

8 个答案:

答案 0 :(得分:9)

您可以将HashMap包装在一个类中,该类委派putget以及您在HashMap中使用的其他方法。这种方法既浪费又安全,因为它不依赖于HashMapAbstractMap的内部实现。以下代码说明了putget委托:

    public class Table {
       protected java.util.HashMap<String, Integer> map = 
             new java.util.HashMap<String, Integer>();

       public Integer get(String key) { return map.get(key); }

       public Integer put(String key, Integer value) {
          if (map.containsKey(key)) {
           // implement the logic you need here.
           // You might want to return `value` to indicate
           // that no changes applied
           return value;
          } else {
            return map.put(key, value);
          }
       }
       // other methods goes here
    }

另一个选择是创建一个扩展HashMap的类,并依赖于它的内部实现。 Java 1.6源代码显示put仅在putAll的{​​{1}}中调用,因此您只需覆盖HashMap方法:

put

另一个选项与第一个选项类似,可以在您的类中创建一个包含 public class Table extends java.util.HashMap<String, Integer> { public Integer put(String key, Integer value) { if (containsKey(key)) { // implement the logic you need here. // You might want to return `value` to indicate // that no changes applied return value; } else { return super.put(key, value); } } } 实例的实用程序方法,并在需要的地方调用该方法:

HashMap

这是一个“内联”等效的手动检查。

答案 1 :(得分:3)

我注意到你通过建议你可能有“100000000个元素”来澄清这个问题。 您仍然不会在HashMap 中有重复项,因为正如其他两张海报所指出的那样,您无法在Map中获得重复的密钥。我仍然不确定我们是否理解这个问题,因为你完全不清楚如何生成标题为“输出”的块,或者你打算用它做什么。

答案 2 :(得分:1)

这可能是一个老问题,但我想与此分享我的经验。正如其他人指出的那样,你不能在HashMap中拥有相同的元素。默认情况下,HashMap不允许这样做,但是在某些情况下你最终可能会遇到两个或多个你不接受的元素,但HashMap会这样。例如,下面的代码定义了一个HashMap,它将一个整数数组作为键,然后添加:

HashMap<int[], Integer> map1 = new HashMap<>();
int[] arr = new int[]{1,2,3};
map1.put(arr, 4);
map1.put(arr, 4);
map1.put(arr, 4);

此时,HashMap不允许对密钥进行公开,map1.size()将返回1.但是,如果添加元素而不创建数组,则首先会有不同之处:

HashMap<int[], Integer> map2 = new HashMap<>();
map2.put(new int[]{4,5,6}, 6);
map2.put(new int[]{4,5,6}, 6);
map2.put(new int[]{4,5,6}, 6);

这样,HashMap将添加所有三个新元素,因此map2.size()将返回3而不是预期的1。

解释是,在第一个地图中,我创建了一次对象arr,并试图将相同的对象添加3次,默认情况下HashMap不允许,因此只考虑最后一次使用。但是,使用第二个映射,我会在堆栈上重新创建一个新对象。创建的三个对象是不同的,并且分开认为它们中的三个具有相同的数据但它们是不同的。这就是为什么HashMap允许它们作为不同的密钥。

最重要的是,您不需要阻止HashMap添加被公开的密钥,因为它不是设计的。但是,您必须注意如何定义这些键,因为故障可能在您身边。

答案 3 :(得分:0)

List<String> keys = new ArrayList<String>(); (1000000)
List<String> values = new ArrayList<String>(); (1000000)
Map<String, String> map = new HashMap<String, String>();
int i =0;
for(String key : keys){
  String returnedValue = map.put(key, values.get(i));
  if(returnedValue!=null){
         map.put(key, returnedValue);
         system.out.println("Duplicate key trying to be entered with new value so   reverting the duplicate key ="+key+"new Value"+values.get(i));
  }
}

答案 4 :(得分:0)

不幸的是,这是 Map 的工作方式。 最简单的解决方法是先调用 hm.remove() 删除所有预先存在的键及其值!像这样:

for (String name : names) {
     hm.remove(name);
     hm.put(name,uri.getQueryParameter(name));
}

如果你不使用 for loop 就这样称呼它:

 hm.remove("John");
 hm.put("John","1");
 hm.remove("Alex");
 hm.put("Alex","2");
 hm.remove("Mike");
 hm.put("Mike","3");
 
 And so on ...
 

答案 5 :(得分:-1)

即使您多次写入相同的键值,您也会看到唯一的一组对。通过迭代或通过执行hm.size();

来检查

答案 6 :(得分:-1)

if(hm.put("John","1") != null)
{
  // "John" was already a key in the map.  The sole value for this key is now "1".
}

答案 7 :(得分:-1)

List<Object> yourElements = new ... // 10000000
for(Object O : yourElements) {
 if(myMap.get(O.key)==null) {
    myMap.put(O.key,O);
 }
}