用于防止HashMap / HashTable中重复的对的Java代码

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

HashMap hm = new HashMap();
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 Pairs
以下条件是满意的。
1> hm.put("John","1"); is not accepted/added again in the Map 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 hm.put("John","2"); is not accepted/added again in the Map

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

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

  public class Table { protected java.util.HashMap map = new java.util.HashMap(); 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仅在HashMap中的putAll中调用,因此您可以简单地覆盖put方法:

  public class Table extends java.util.HashMap { 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实例的实用程序方法,并在需要向您的地图添加内容的地方调用该方法:

 public final Integer putToMap(String key, String value) { if(this.map.containsKey(key)) { return value; } else { return this.map.put(key, value); } } 

这是手动检查的“内联”等价物。

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

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

 HashMap 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 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添加被公开的密钥,因为它不是设计的。 但是,您必须注意如何定义这些键,因为故障可能在您身边。

 List keys = new ArrayList(); (1000000) List values = new ArrayList(); (1000000) Map map = new HashMap(); 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)); } } 

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

 if(hm.put("John","1") != null) { // "John" was already a key in the map. The sole value for this key is now "1". } 
 List yourElements = new ... // 10000000 for(Object O : yourElements) { if(myMap.get(O.key)==null) { myMap.put(O.key,O); } }