zoukankan      html  css  js  c++  java
  • 转: 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

  • 相关阅读:
    了解jQuery
    了解JavaScript
    了解DOM
    了解CSS
    UICollectionViewCell点击高亮效果(附带效果GIF)
    产品迭代缓慢的原因
    了解Web的相关知识
    HTML常用标签
    HTML常用标签效果展示
    了解数据产品经理
  • 原文地址:https://www.cnblogs.com/ibearpig/p/3633611.html
Copyright © 2011-2022 走看看