Guest User

PairMap

a guest
Jan 15th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4.  
  5. public  class PairMap<K,V> {
  6.  
  7.     //Holds Values of Keys.
  8.     //K = key, V = value.
  9.    
  10.     /**
  11.      *  keyMap holds the Keys to Values
  12.      */
  13.     public HashMap<K,V> keyMap = new HashMap<K,V>();
  14.     /**
  15.      *  valueMap holds Values deposited in keyMap as Keys holding the set of keys they are mapped to
  16.      * in keyMap.
  17.      *
  18.      * Example: <String,Integer> keyMap would have "hello world" mapped to 0.
  19.      * the valueMap would have 0 hold "hello world" within a set of other keys also mapped to 0 in keyMap
  20.      */
  21.     public HashMap<V,Set<K>> valueMap = new HashMap<V,Set<K>>();
  22.    
  23.     public PairMap(){
  24.        
  25.     }
  26.    
  27.    
  28.    
  29.     public V put(K key, V value){
  30.         if(getValueMap(getKeyMap(key)) != null){
  31.         getValueMap(getKeyMap(key)).remove(key);
  32.         if(getValueMap(getKeyMap(key)).isEmpty()){
  33.             removeValueMap(getKeyMap(key));
  34.         }
  35.         }
  36.         if(keyMap.containsKey(key)){///////////////////////////////////////////////
  37.             if(getValueMap(value) == null){
  38.                 valueMap.put(value, new HashSet<K>());
  39.             }
  40.             getValueMap(value).add(key);                                            ///
  41.         }else{                                                  //Adds value to valueMap as a key if the key exists.
  42.             if(getValueMap(value) == null){
  43.             valueMap.put(value, new HashSet<K>());
  44.             }           //If it doesn't exist, place the set of keys to allow for adding into first.
  45.             getValueMap(value).add(key);                                            ///
  46.         }//////////////////////////////////////////////////////////////////////////////    
  47.  
  48.         keyMap.put(key, value);    
  49.        
  50.         return value;
  51.     }
  52.    
  53.     public V removeKey(K key){     
  54.         if(keyMap.containsKey(key)){           
  55.             if(getValueMap(getKeyMap(key)).size() == 1){
  56.                 removeValueMap(getKeyMap(key));
  57.             }else{
  58.                 getValueMap(getKeyMap(key)).remove(key);
  59.             }
  60.             return removeKeyMap(key);
  61.         }
  62.         return null;
  63.     }
  64.    
  65.     @SuppressWarnings("unchecked")
  66.     public Set<K> removeValue(V val){
  67.        
  68.         if(keyMap.containsValue(val)){
  69.            
  70.             Object[] fill = getValueMap(val).toArray();
  71.            
  72.             for(int i = 0; i < fill.length; i++){
  73.                 removeKeyMap((K)fill[i]);
  74.             }          
  75.             return removeValueMap(val);        
  76.         }
  77.         return null;
  78.     }
  79.    
  80.     public boolean containsKey(K key){
  81.         return keyMap.containsKey(key);
  82.     }
  83.    
  84.     public boolean containsValue(V val){
  85.         return keyMap.containsValue(val);
  86.     }
  87.    
  88.     public Set<K> getKeysOfValue(V val){
  89.         return getValueMap(val);
  90.     }
  91.    
  92.     public Set<K> getKeys(){
  93.         return keyMap.keySet();
  94.     }
  95.    
  96.     public Set<V> getValues(){
  97.         return valueMap.keySet();
  98.     }
  99.    
  100.     public boolean valueIsMappedToKey(K key, V val){
  101.         return getValueMap(val).contains(key);
  102.     }
  103.    
  104.     @Override
  105.     public String toString(){
  106.         return "Key Map: "+keyMap + "\n" + "Value Map: "+valueMap;
  107.     }
  108.    
  109.     protected V getKeyMap(K key){
  110.         return keyMap.get(key);
  111.     }
  112.    
  113.     protected Set<K> getValueMap(V val){
  114.         return valueMap.get(val);
  115.     }
  116.    
  117.     protected V removeKeyMap(K key){       
  118.         return keyMap.remove(key);     
  119.     }
  120.    
  121.     protected Set<K> removeValueMap(V val){
  122.         return valueMap.remove(val);
  123.     }
  124.    
  125.     public V get(K key){
  126.         return getKeyMap(key);
  127.     }
  128.  
  129.     public void clear() {
  130.         keyMap.clear();
  131.         valueMap.clear();
  132.     }
  133.  
  134.     public int size() {
  135.         return this.keyMap.keySet().size();
  136.     }
  137.    
  138.     /**Returns the first available key at the value.
  139.      * valueMap.get(VALUE).toArray()[0] <--
  140.      * If it is in the valueMap.
  141.      * */
  142.     @SuppressWarnings("unchecked")
  143.     public K getAvailableKeyOfValue(V val){
  144.         if(valueMap.containsKey(val)){
  145.             return (K) valueMap.get(val).toArray()[0];
  146.         }
  147.         System.out.println(this.getClass().getName()+": Null Key of Value.");
  148.         return null;
  149.     }
  150.    
  151. }
Add Comment
Please, Sign In to add comment