簡單組合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. }  

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章