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

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

 

接口定义:下载 

Java代码   
  1. package cc.lixiaohui.demo.javassist.proxy.util;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.Set;  
  5.   
  6. /** 
  7.  * 两个键的复合map 
  8.  * <pre> 
  9.  * key------+ 
  10.  *          |-->value 
  11.  * param----+ 
  12.  * <pre> 
  13.  *  
  14.  * @author lixiaohui 
  15.  * @date 2016年10月1日 上午10:58:40 
  16.  *  
  17.  */  
  18. public interface CompoundKeyMap<K, P, V> {  
  19.       
  20.     V get(K key, P param);  
  21.     V get(K key, P param, V defValue);  
  22.       
  23.     V put(K key, P param, V value);  
  24.     V putIfAbsent(K key, P param, V value);  
  25.       
  26.     Set<java.util.Map.Entry<CompoundKey<K, P>, V>> entrySet();  
  27.     Set<CompoundKey<K, P>> keys();  
  28.     Collection<V> values();  
  29.       
  30.     int size();  
  31.     boolean isEmpty();  
  32.       
  33.     public interface CompoundKey<K, P> {  
  34.         K getKey();  
  35.         P getParam();  
  36.     }  
  37.       
  38. }  

 

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

 

Java代码   
  1. package cc.lixiaohui.demo.javassist.proxy.util;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7. import java.util.Objects;  
  8. import java.util.Set;  
  9.   
  10. /** 
  11.  * 基于{@link java.util.HashMap}的CompoundKeyMap的实现. 
  12.  *  
  13.  * @author lixiaohui 
  14.  * @date 2016年10月1日 下午12:37:08 
  15.  *  
  16.  */  
  17. public class CompoundKeyHashMap<K, P, V> implements CompoundKeyMap<K, P, V> {  
  18.   
  19.     private Map<CompoundKey<K, P>, V> map = new HashMap<CompoundKey<K, P>, V>();  
  20.       
  21.       
  22.     public V get(K key, P param) {  
  23.         key = Objects.requireNonNull(key, "key cannot be null");  
  24.         param = Objects.requireNonNull(param, "param cannot be null");  
  25.           
  26.         return map.get(newKey(key, param));  
  27.     }  
  28.   
  29.     private CompoundKeyMap.CompoundKey<K, P> newKey(K key, P param) {  
  30.         return new CompoundKeyImpl<K, P>(key, param);  
  31.     }  
  32.   
  33.     public V get(K key, P param, V defValue) {  
  34.         key = Objects.requireNonNull(key, "key cannot be null");  
  35.         param = Objects.requireNonNull(param, "param cannot be null");  
  36.           
  37.         V value = get(key, param);  
  38.         return value == null ? defValue : value;  
  39.     }  
  40.   
  41.     public V put(K key, P param, V value) {  
  42.         return map.put(newKey(key, param), value);  
  43.     }  
  44.   
  45.     public V putIfAbsent(K key, P param, V value) {  
  46.         return map.putIfAbsent(newKey(key, param), value);  
  47.     }  
  48.   
  49.     public Set<Entry<CompoundKeyMap.CompoundKey<K, P>, V>> entrySet() {  
  50.         return map.entrySet();  
  51.     }  
  52.   
  53.     public Set<CompoundKeyMap.CompoundKey<K, P>> keys() {  
  54.         return map.keySet();  
  55.     }  
  56.   
  57.     public Collection<V> values() {  
  58.         return map.values();  
  59.     }  
  60.   
  61.     public int size() {  
  62.         return map.size();  
  63.     }  
  64.   
  65.     public boolean isEmpty() {  
  66.         return map.isEmpty();  
  67.     }  
  68.   
  69.     static class CompoundKeyImpl<K, P> implements CompoundKey<K, P> {  
  70.   
  71.         private K key;  
  72.           
  73.         private P param;  
  74.           
  75.         CompoundKeyImpl(K key, P param) {  
  76.             super();  
  77.             this.key = key;  
  78.             this.param = param;  
  79.         }  
  80.   
  81.         public K getKey() {  
  82.             return key;  
  83.         }  
  84.   
  85.         public P getParam() {  
  86.             return param;  
  87.         }  
  88.           
  89.         @Override  
  90.         public int hashCode() {  
  91.             final int prime = 31;  
  92.             int result = 1;  
  93.             result = prime * result + ((key == null) ? 0 : key.hashCode());  
  94.             result = prime * result + ((param == null) ? 0 : param.hashCode());  
  95.             return result;  
  96.         }  
  97.   
  98.         @Override  
  99.         public boolean equals(Object obj) {  
  100.             if (this == obj)  
  101.                 return true;  
  102.             if (obj == null)  
  103.                 return false;  
  104.             if (getClass() != obj.getClass())  
  105.                 return false;  
  106.             CompoundKeyImpl<?, ?> other = (CompoundKeyImpl<?, ?>) obj;  
  107.             if (key == null) {  
  108.                 if (other.key != null)  
  109.                     return false;  
  110.             } else if (!key.equals(other.key))  
  111.                 return false;  
  112.             if (param == null) {  
  113.                 if (other.param != null)  
  114.                     return false;  
  115.             } else if (!param.equals(other.param))  
  116.                 return false;  
  117.             return true;  
  118.         }  
  119.     }  
  120.       
  121. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值