map.put(1,a)
map.put(2,b)
map1.put(a,100);
map1.put(b,200);
我现在遇到的问题是,通过map.get(1)获得了a,然后通过map1.get(a)获得了100,我再将这个值加100得到200,现在如何通过200获得b呢?除了重写一个HashMap外,还能有什么方法能实现这个功能呢?
map.put(2,b)
map1.put(a,100);
map1.put(b,200);
我现在遇到的问题是,通过map.get(1)获得了a,然后通过map1.get(a)获得了100,我再将这个值加100得到200,现在如何通过200获得b呢?除了重写一个HashMap外,还能有什么方法能实现这个功能呢?
比如:
map1.put(b,200);
map1.put(c,200);
map1.put(d,200);它只保证key的唯一性,不保证value的唯一性,所以无法从value查到key
public Object getKey(Map map,Object value) {
Object o = null;
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
if (entry.getValue().equals(value)) {
o = entry.getKey();
}
}
return o;
} @SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
System.out.println(new Map_ValueGetKey().getKey(map,"c"));
}}
Set<K> keySet = map.getSet();
Iterator<K> keyIterator = keySet.getIterator();
K key;
while( keyIterator.hasNext() ) {
key = keyIterator.next();
if( map.get(key).equals( value ) ) {
return k;
}
}
return null;
}
不过标记@SuppressWarnings("unchecked")是什么意思呀?