日期:2014-05-20  浏览次数:20723 次

请问在map集合中通过value怎么得到key?
请问在map集合中通过value怎么得到key?

------解决方案--------------------
Java code

        HashMap map = new HashMap();
        map.put("1", "11");
        map.put("2", "22");
        map.put("3", "33");
        map.put("4", "44");
        map.put("5", "55");
        map.put("6", "66");
        map.put("7", "77");
        int num = 0 ;
        String key;
        String value;
        Iterator it1 = map.keySet().iterator();
        Iterator it2 = map.entrySet().iterator();
        
        while(it2.hasNext()){
            num++;
            value = ((Object) it2.next()).toString(); 
            if("55".equals(value))
                break;
        }

        System.out.println("num = " + num);
        while(it1.hasNext()){
            num--;
            value = (String) it1.next(); 
            if(num ==0){
                System.out.println("value = " + value);
                break;
            }
        }

------解决方案--------------------
继承 HashMap,覆盖一些方法:

Java code
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ValueHashMap<K, V> extends HashMap<K, V> {

    private static final long serialVersionUID = 4136683943755968213L;
    
    protected Map<V, Set<K>> valueMap;
    
    protected boolean isDebug = false;

    public ValueHashMap() {
        super();
        initValueMap();
    }

    public ValueHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        initValueMap();
    }

    public ValueHashMap(int initialCapacity) {
        super(initialCapacity);
        initValueMap();
    }

    public ValueHashMap(Map<? extends K, ? extends V> m) {
        super(m);
        initValueMap();
    }

    private void initValueMap() {
        valueMap = new HashMap<V, Set<K>>();
    }

    public void clear() {
        super.clear();
        valueMap.clear();
    }

    public V put(K key, V value) {
        V v = super.put(key, value);
        putValue(key, value);
        debugValueMap(key, value);
        return v;
    }

    public void putAll(Map<? extends K, ? extends V> m) {
        super.putAll(m);
        for(Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
            putValue(entry.getKey(), entry.getValue());
            debugValueMap(entry.getKey(), entry.getValue());
        }        
    }

    public V remove(Object key) {
        if(!containsKey(key)) {
            return null;
        }
        V v = super.remove(key);
        removeValueMapKey(key, v);
        debugValueMap(key, null);
        return v;
    }
    
    public Set<K> getKeySet(V value) {
        return Collections.unmodifiableSet(valueMap.get(value));
    }
    
    protected void putValue(K key, V value) {
        Set<K> keys = valueMap.get(value);
        if(keys == null) {
            keys = new HashSet<K>();
            valueMap.put(value, keys);
        }
        keys.add(key);
    }
    
    protected void removeValueMapKey(Object key, V value) {
        valueMap.get(value).remove(key);
    }
    
    protected void debugValueMap(Object key, V value) {
        if(!isDebug) {
            return;
        }
        StackTraceElement se = new Throwable().getStackTrace()[1];
        System.out.print(se.getMethodName() + ": ");
        System.out.print("key = [" + key + "]");
        if(!"remove".equals(se.getMethodName())) {
            System.out.print(", value = [" + value + "]");
        }
        System.out.println();
        for(Map.Entry<V, Set<K>> en : valueMap.entrySet()) {
            System.out.print("  [" + en.getKey() + "] --> ");
            int i = 0;
            for(K k : en.getValue()) {
                if(i++ > 0) {
                    System.out.print(", ");
                }
                System.out.print("[" + k + "]");
            }
            System.out.println();
        }
        System.out.println();
    }
}