Skip to content

Instantly share code, notes, and snippets.

@FarisR99
Last active August 29, 2015 14:04
Show Gist options
  • Save FarisR99/f19ea24f06a3813d4f34 to your computer and use it in GitHub Desktop.
Save FarisR99/f19ea24f06a3813d4f34 to your computer and use it in GitHub Desktop.
CustomMap
Advantages:
- It has a removeValue() feature.
- Any normal "voids" would return CustomMap instead, so that you can keep stuff on one line. E.g. CustomMap<String, String> customMap = new CustomMap<String, String>().putAll(theMap);
- It has a getKey() by value feature.
- The method names are neater.
- Like Java 8, it has support for optional default values, otherwise null.
- Support for normal Map class (customMap.toMap() and new CustomMap(map)).
Disadvantages:
- The removeValue() code looks ugly.
- Worse performance than Map.
Adding comments soon!
import java.util.*;
public class CustomMap<K, V> implements Cloneable {
private List<K> keys = null;
private List<V> values = null;
public CustomMap() {
this.keys = new ArrayList<K>();
this.values = new ArrayList<V>();
}
public CustomMap(Map<K, V> duplicateMap) {
this.keys = new ArrayList<K>();
this.values = new ArrayList<V>();
this.keys.addAll(duplicateMap.keySet());
this.values.addAll(duplicateMap.values());
}
public CustomMap clear() {
this.keys.clear();
this.values.clear();
return this;
}
public CustomMap<K, V> clone() {
return new CustomMap<K, V>().putAll(this);
}
public Set<CustomEntry<K, V>> entrySet() {
Set<CustomEntry<K, V>> entries = new LinkedHashSet<CustomEntry<K, V>>();
for (int i = 0; i < this.getSize(); i++)
entries.add(new CustomEntry<K, V>(this.keys.get(i), this.values.get(i)));
return entries;
}
public V get(K key) {
return this.getValue(key);
}
public V get(K key, V defaultValue) {
return this.getValue(key, defaultValue);
}
public K getKey(V value) {
return this.values.contains(value) ? this.keys.get(this.values.indexOf(value)) : null;
}
public Set<K> getKeys() {
return new LinkedHashSet<K>(this.keys);
}
public int getSize() {
return (this.keys.size() + this.values.size()) / 2;
}
public V getValue(K key) {
return this.keys.contains(key) ? this.values.get(this.keys.indexOf(key)) : null;
}
public V getValue(K key, V defaultValue) {
return this.keys.contains(key) ? this.values.get(this.keys.indexOf(key)) : defaultValue;
}
public Set<V> getValues() {
return new LinkedHashSet<V>(this.values);
}
public boolean isEmpty() {
return this.keys.isEmpty() && this.values.isEmpty();
}
public CustomMap put(K key, V value) {
this.keys.add(key);
this.values.add(value);
return this;
}
public CustomMap putAll(CustomMap<K, V> customMap) {
if (customMap != null) {
for (CustomEntry<K, V> customEntry : customMap.entrySet()) {
this.keys.add(customEntry.getKey());
this.values.add(customEntry.getValue());
}
}
return this;
}
public V remove(K key) {
return this.removeKey(key);
}
public V removeKey(K key) {
V oldValue = null;
if (this.keys.contains(key)) {
oldValue = this.values.remove(this.keys.indexOf(key));
this.keys.remove(key);
}
return oldValue;
}
public List<K> removeValue(V value) {
List<K> oldKeys = null;
if (this.values.contains(value)) {
oldKeys = new ArrayList<K>();
List<V> toRemove = new ArrayList<V>();
for (int i = 0; i < this.values.size(); i++) {
V iValue = this.values.get(i);
if ((iValue == null && value == null) || (iValue != null && iValue.equals(value))) {
toRemove.add(iValue);
oldKeys.add(this.keys.remove(i));
}
}
this.values.removeAll(toRemove);
}
return oldKeys;
}
public Map<K, V> toMap() {
Map<K, V> kvMap = new LinkedHashMap<K, V>();
for (CustomEntry<K, V> customEntry : this.entrySet()) {
kvMap.put(customEntry.getKey(), customEntry.getValue());
}
return kvMap;
}
public class CustomEntry<K, V> implements Map.Entry {
private K key = null;
private V value = null;
public CustomEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return this.key;
}
@Override
public V getValue() {
return this.value;
}
@Override
public V setValue(Object value) {
V oldValue = this.value;
this.value = (V) value;
return oldValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment