用于防止重复< Key,Value>的Java代码HashMap / HashTable中的对 [英] Java code to Prevent duplicate <Key,Value> pairs in HashMap/HashTable
问题描述
我有一个HashMap如下(假设它有10,0000个元素)
I have a HashMap as below (assuming it has 10,0000 elements)
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(代码,5);
==========================
Expected Output
==========================
Key = John,Value =1
Key = Alex,Value =2
Key = Mike,Value =3
Key = Justin,值=4
键=代码,值=5
= ==========================
Key = John",Value = "1"
Key = Alex",Value = "2"
Key = Mike",Value = "3"
Key = Justin",Value = "4"
Key = Code",Value = "5"
===========================
我需要Java代码来防止在HashMap中添加重复< Key,Value>对
,例如:
以下条件是满足的。
1> hm.put(John,1);在地图中不再接受/添加
2> hm.put(约翰 2 );不接受/在地图中再次添加
I need Java code to prevent Addition of Duplicate <Key,Value> Pairs
in HashMap such
that below conditions are staisfied.
1> 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代码将不胜感激。(因为我可以向现有地图添加任何副本,所以需要通用解决方案)
Hope its clear. Java code provided will be appreciated.(generic solution needed since i can add any duplicate to the existing map)
推荐答案
您可以在一个类中包装 HashMap
,该代理 put
, get
,以及您从 HashMap
中使用的其他方法。这种方法既浪费又安全,因为它不依赖于 HashMap
, AbstractMap
的内部实现。下面的代码说明 put
, get
委托:
You can wrap HashMap
in a class, which delegates put
, get
, and other methods you use from HashMap
. This method is wasteful but safe, since it doesn't depend on the internal implementation of HashMap
, AbstractMap
. The code below illustrates put
, get
delegating:
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
中的 HashMap中调用
,所以你可以简单地覆盖 put
方法:
Another option is to make a class which extends HashMap
, and depend on its internal implementation. Java 1.6 sources shows that put
is called only in putAll
in HashMap
, so you can simply override put
method:
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
实例的实用程序方法,并在你需要的地方调用该方法:
Another option is similar to the first, and can make an utility method in your class which contains the HashMap
instance and call that method wherever you need put something to your map:
public final Integer putToMap(String key, String value) {
if(this.map.containsKey(key)) {
return value;
} else {
return this.map.put(key, value);
}
}
这是一个内联等效的手动检查。
This is an "inline" equivalent of checking manually.
这篇关于用于防止重复< Key,Value>的Java代码HashMap / HashTable中的对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!