您的位置:首页 > 编程语言 > Java开发

简单组合java.util.Map<K,V>实现Map<K,P,V>

2017-09-11 15:23 489 查看


简单组合java.util.Map<K,V>实现Map<K,P,V>

 

java.util.Map<K,V>为单键对单值,有时需要双键对单值,因此基于Map<K,V>可以简单实现一个Map<K,P,V>。

 

接口定义:

Java代码 

package cc.lixiaohui.demo.javassist.proxy.util;  

  

import java.util.Collection;  

import java.util.Set;  

  

/** 

 * 两个键的复合map 

 * <pre> 

 * key------+ 

 *          |-->value 

 * param----+ 

 * <pre> 

 *  

 * @author lixiaohui 

 * @date 2016年10月1日 上午10:58:40 

 *  

 */  

public interface CompoundKeyMap<K, P, V> {  

      

    V get(K key, P param);  

    V get(K key, P param, V defValue);  

      

    V put(K key, P param, V value);  

    V putIfAbsent(K key, P param, V value);  

      

    Set<java.util.Map.Entry<CompoundKey<K, P>, V>> entrySet();  

    Set<CompoundKey<K, P>> keys();  

    Collection<V> values();  

      

    int size();  

    boolean isEmpty();  

      

    public interface CompoundKey<K, P> {  

        K getKey();  

        P getParam();  

    }  

      

}  

 

基于HashMap的简单实现,关键在于CompoundKey的hashcode和equals方法的重写:

 

Java代码 

package cc.lixiaohui.demo.javassist.proxy.util;  

  

import java.util.Collection;  

import java.util.HashMap;  

import java.util.Map;  

import java.util.Map.Entry;  

import java.util.Objects;  

import java.util.Set;  

  

/** 

 * 基于{@link java.util.HashMap}的CompoundKeyMap的实现. 

 *  

 * @author lixiaohui 

 * @date 2016年10月1日 下午12:37:08 

 *  

 */  

public class CompoundKeyHashMap<K, P, V> implements CompoundKeyMap<K, P, V> {  

  

    private Map<CompoundKey<K, P>, V> map = new HashMap<CompoundKey<K, P>, V>();  

      

      

    public V get(K key, P param) {  

        key = Objects.requireNonNull(key, "key cannot be null");  

        param = Objects.requireNonNull(param, "param cannot be null");  

          

        return map.get(newKey(key, param));  

    }  

  

    private CompoundKeyMap.CompoundKey<K, P> newKey(K key, P param) {  

        return new CompoundKeyImpl<K, P>(key, param);  

    }  

  

    public V get(K key, P param, V defValue) {  

        key = Objects.requireNonNull(key, "key cannot be null");  

        param = Objects.requireNonNull(param, "param cannot be null");  

          

        V value = get(key, param);  

        return value == null ? defValue : value;  

    }  

  

    public V put(K key, P param, V value) {  

        return map.put(newKey(key, param), value);  

    }  

  

    public V putIfAbsent(K key, P param, V value) {  

        return map.putIfAbsent(newKey(key, param), value);  

    }  

  

    public Set<Entry<CompoundKeyMap.CompoundKey<K, P>, V>> entrySet() {  

        return map.entrySet();  

    }  

  

    public Set<CompoundKeyMap.CompoundKey<K, P>> keys() {  

        return map.keySet();  

    }  

  

    public Collection<V> values() {  

        return map.values();  

    }  

  

    public int size() {  

        return map.size();  

    }  

  

    public boolean isEmpty() {  

        return map.isEmpty();  

    }  

  

    static class CompoundKeyImpl<K, P> implements CompoundKey<K, P> {  

  

        private K key;  

          

        private P param;  

          

        CompoundKeyImpl(K key, P param) {  

            super();  

            this.key = key;  

            this.param = param;  

        }  

  

        public K getKey() {  

            return key;  

        }  

  

        public P getParam() {  

            return param;  

        }  

          

        @Override  

        public int hashCode() {  

            final int prime = 31;  

            int result = 1;  

            result = prime * result + ((key == null) ? 0 : key.hashCode());  

            result = prime * result + ((param == null) ? 0 : param.hashCode());  

            return result;  

        }  

  

        @Override  

        public boolean equals(Object obj) {  

            if (this == obj)  

                return true;  

            if (obj == null)  

                return false;  

            if (getClass() != obj.getClass())  

                return false;  

            CompoundKeyImpl<?, ?> other = (CompoundKeyImpl<?, ?>) obj;  

            if (key == null) {  

                if (other.key != null)  

                    return false;  

            } else if (!key.equals(other.key))  

                return false;  

            if (param == null) {  

                if (other.param != null)  

                    return false;  

            } else if (!param.equals(other.param))  

                return false;  

            return true;  

        }  

    }  

      

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: