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外,还能有什么方法能实现这个功能呢?

解决方案 »

  1.   

    你得重新写一个HashMap要注意,HashMap不是双向1对1的它可以是多对1的
    比如:
    map1.put(b,200);
    map1.put(c,200);
    map1.put(d,200);它只保证key的唯一性,不保证value的唯一性,所以无法从value查到key
      

  2.   

    我知道HashMap不是双向1对1的,我只是回答1楼的仁兄,我保证我的数据是双向1对1的。我是想问除了重写个HashMap外,还有什么办法呢?或者不用Map也行,只要能实现这个功能即可。
      

  3.   

    可以试试这个。public class Map_ValueGetKey { @SuppressWarnings("unchecked")
    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"));
    }}
      

  4.   

    不知道这样行不行public K getKey( Map<K,V> map, V value ) {
        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;
    }
      

  5.   

    和6楼差不多呀。
    不过标记@SuppressWarnings("unchecked")是什么意思呀?