see613

Example #1: MultiValueMap

Nov 5th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.82 KB | None | 0 0
  1. package see613.core.datatypes;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. /**
  9.  * HashMap of key => list of values
  10.  */
  11. public class MultiValueMap<K, V> extends HashMap<K, List<V>> {
  12.     private static final long serialVersionUID = 616L;
  13.    
  14.     private V defaultValue = null;
  15.    
  16.  
  17.     public MultiValueMap(Map<? extends K, ? extends List<V>> m) {
  18.         super(m);
  19.     }
  20.    
  21.     public MultiValueMap(int initialCapacity, float loadFactor) {
  22.         super(initialCapacity, loadFactor);    
  23.     }
  24.  
  25.     public MultiValueMap(int initialCapacity) {
  26.         super(initialCapacity);
  27.     }
  28.  
  29.     public MultiValueMap() {
  30.         super();
  31.     }
  32.    
  33.     public List<V> put(K key, V val) {
  34.         return put(key, val, false);
  35.     }
  36.    
  37.     public List<V> put(K key, V val, boolean withRepeats) {
  38.         List<V> list;
  39.        
  40.         if ( !containsKey(key) ) {
  41.             list = new ArrayList<V>();
  42.            
  43.             put(key, list);
  44.         }
  45.         else {
  46.             list = get(key);
  47.         }
  48.    
  49.         if ( withRepeats || !list.contains(val) ) {
  50.             list.add(val);
  51.         }
  52.         return list;
  53.     }
  54.    
  55.     public List<V> get(K key) {
  56.         return ( containsKey(key) ) ? super.get(key) : new ArrayList<V>();
  57.     }
  58.    
  59.     public V getOne(K key, int subIndex) {
  60.         V value = defaultValue;
  61.        
  62.         if ( containsKey(key) ) {
  63.             List<V> list = get(key);
  64.            
  65.             if ( list.size() > subIndex ) {
  66.                 value = list.get(subIndex);
  67.             }
  68.         }              
  69.         return value;
  70.     }
  71.    
  72.     public V getOne(K key) {
  73.         return getOne(key, 0);
  74.     }
  75.    
  76.     public int sizeById(K key) {
  77.         return get(key).size();
  78.     }
  79.    
  80.     public V remove(K key, int subKey) {
  81.         V value = null;
  82.                
  83.         if ( containsKey(key) ) {
  84.             List<V> list = get(key);           
  85.             int subKeyIndex = list.indexOf(subKey);
  86.            
  87.             if ( subKeyIndex > -1 ) {
  88.                 value = list.remove(subKeyIndex);
  89.             }
  90.            
  91.             if ( list.size() == 0 ) {
  92.                 remove(key);
  93.             }
  94.         }      
  95.         return value;
  96.     }
  97.    
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment