HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>();
我可以有像这样的值:
(1 , 7)
(2 , 4)
(4 , 5)
(3, 7)
不会出现重复的键。只会出现重复的值。
我想选择具有重复值的(Key,Value)对。
如果我能得到另一个HashMap作为重复的(Key,Value)对,那就太好了。我该怎么做?
我期望的输出:
(1 , 7)
(3, 7)
HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>();
我可以有像这样的值:
(1 , 7)
(2 , 4)
(4 , 5)
(3, 7)
不会出现重复的键。只会出现重复的值。
我想选择具有重复值的(Key,Value)对。
如果我能得到另一个HashMap作为重复的(Key,Value)对,那就太好了。我该怎么做?
我期望的输出:
(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());
}
}
HashMap<value, HashMap<key,value>>
。 - Jin35仅概述一下......
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
}
}