HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>();
我可以有像
(1 , 7)
(2 , 4)
(4 , 5)
(3, 7)
不会有任何重复的 Keys 。只能出现重复值
我想选择具有重复值的 (Key,Value) 对。
如果我将 Duplicate (Key,Value) 作为另一个 Hashmap,那就太好了。我该怎么做。?
我期待输出
(1 , 7)
(3, 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}
您不能有重复的键。把它想象成一堆盒子,每个盒子里都有一个点。您可以在盒子 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;
}
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());
}
}
只是为了给大纲...
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 然后忽略我的..