Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class BothWaysMap<K,V> {
- private List<K> keys;
- private List<V> values;
- public BothWaysMap(){
- keys = new ArrayList<K>();
- values = new ArrayList<V>();
- }
- public synchronized void put(K key, V value){
- if (keys.contains(key)) remove(key);
- keys.add(key);
- values.add(value);
- }
- public synchronized void insert(K key, V value, int index){
- if (keys.contains(key)) remove(key);
- keys.add(index, key);
- values.add(index, value);
- }
- public synchronized void remove(K key){
- values.remove(get(key));
- keys.remove(key);
- }
- public synchronized void clear(){
- keys.clear();
- values.clear();
- }
- public int size(){
- return keys.size();
- }
- public List<K> getKeys(){
- return keys;
- }
- public List<V> getValues(){
- return values;
- }
- public boolean containsKey(K key){
- return keys.contains(key);
- }
- public boolean containsValue(V value){
- return values.contains(value);
- }
- public int indexOfKey(K key){
- return keys.indexOf(key);
- }
- public int indexOfValue(V value){
- return values.indexOf(value);
- }
- public V get(K key){
- if (!containsKey(key)) return null;
- return values.get(indexOfKey(key));
- }
- public K getKey(V value){
- if (!containsValue(value)) return null;
- return keys.get(indexOfValue(value));
- }
- public K getKeyAt(int index){
- return keys.get(index);
- }
- public V getValueAt(int index){
- return values.get(index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement