Advertisement
Guest User

BothWaysMap.java

a guest
Sep 23rd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class BothWaysMap<K,V> {
  5.     private List<K> keys;
  6.     private List<V> values;
  7.    
  8.     public BothWaysMap(){
  9.         keys = new ArrayList<K>();
  10.         values = new ArrayList<V>();
  11.     }
  12.    
  13.     public synchronized void put(K key, V value){
  14.         if (keys.contains(key)) remove(key);
  15.         keys.add(key);
  16.         values.add(value);
  17.     }
  18.    
  19.     public synchronized void insert(K key, V value, int index){
  20.         if (keys.contains(key)) remove(key);
  21.         keys.add(index, key);
  22.         values.add(index, value);
  23.     }
  24.    
  25.     public synchronized void remove(K key){
  26.         values.remove(get(key));
  27.         keys.remove(key);
  28.     }
  29.    
  30.     public synchronized void clear(){
  31.         keys.clear();
  32.         values.clear();
  33.     }
  34.    
  35.     public int size(){
  36.         return keys.size();
  37.     }
  38.    
  39.     public List<K> getKeys(){
  40.         return keys;
  41.     }
  42.    
  43.     public List<V> getValues(){
  44.         return values;
  45.     }
  46.    
  47.     public boolean containsKey(K key){
  48.         return keys.contains(key);
  49.     }
  50.    
  51.     public boolean containsValue(V value){
  52.         return values.contains(value);
  53.     }
  54.    
  55.     public int indexOfKey(K key){
  56.         return keys.indexOf(key);
  57.     }
  58.    
  59.     public int indexOfValue(V value){
  60.         return values.indexOf(value);
  61.     }
  62.    
  63.     public V get(K key){
  64.         if (!containsKey(key)) return null;
  65.         return values.get(indexOfKey(key));
  66.     }
  67.    
  68.     public K getKey(V value){
  69.         if (!containsValue(value)) return null;
  70.         return keys.get(indexOfValue(value));
  71.     }
  72.    
  73.     public K getKeyAt(int index){
  74.         return keys.get(index);
  75.     }
  76.    
  77.     public V getValueAt(int index){
  78.         return values.get(index);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement