• 转: java 双向map


    package tools;
    
    import java.util.HashMap;
    
    public class DuplexMap<K,V> {
        class Entry{
            K k;
            V v;
            public Entry(K k,V v){
                this.k=k;
                this.v=v;
            }
            public K getK() {
                return k;
            }
            public V getV() {
                return v;
            }
            public void setK(K k) {
                this.k = k;
            }
            public void setV(V v) {
                this.v = v;
            }
        }
        private HashMap<K,Entry> kEntyMap=new HashMap<K,Entry>();
        private HashMap<V,Entry> vEntyMap=new HashMap<V,Entry>();
        public boolean contains(K k){
            return kEntyMap.containsKey(k);
        }
        public boolean containsValue(V v){
            return vEntyMap.containsKey(v);
        }
        public V getByKey(K k){
            Entry e=kEntyMap.get(k);
            if(e==null){
                return null;
            }
            return e.getV();
        }
        public K getbyValue(V v){
            Entry e=vEntyMap.get(v);
            if(e==null){
                return null;
            }
            return e.getK();
        }
        public boolean put(K k,V v){
            if(k==null||v==null){
                return false;
            }
            Entry e=new Entry(k, v);
            if(contains(k)){
                remove(k);
            }
            if(containsValue(v)){
                removeByValue(v);
            }
            kEntyMap.put(k, e);
            vEntyMap.put(v, e);
            return true;
        }
        public V remove(K k){
            Entry e=kEntyMap.remove(k);
            if(e==null){
                return null;
            }
            vEntyMap.remove(e.getV());
            return e.getV();
        }
        public K removeByValue(V v){
            Entry e=vEntyMap.remove(v);
            if(e==null){
                return null;
            }
            kEntyMap.remove(e.getK());
            return e.getK();
        }
    }

    转自:http://www.oschina.net/code/snippet_83492_4187

  • 相关阅读:
    P4014 分配问题 网络流
    P4015 运输问题 网络流问题
    P4013 数字梯形问题 网络流
    网络流 P2770 航空路线问题
    网络流之最小费用最大流 P1251 餐巾计划问题
    二分图定理
    数论 C
    网络流 E
    网络流 之 P2766 最长不下降子序列问题
    scp使用
  • 原文地址:https://www.cnblogs.com/ibearpig/p/3633611.html
Copyright © 2020-2023  润新知