42 lines
1.0 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: containers/MapEntry.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// A simple Map.Entry for sample Map implementations.
import java.util.*;
public class MapEntry<K,V> implements Map.Entry<K,V> {
private K key;
private V value;
public MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public K getKey() { return key; }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public V getValue() { return value; }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public V setValue(V v) {
V result = value;
value = v;
return result;
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public int hashCode() {
return (key==null ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public boolean equals(Object o) {
if(!(o instanceof MapEntry)) return false;
@SuppressWarnings("unchecked")
MapEntry<K,V> me = (MapEntry<K,V>)o;
2015-04-20 15:36:01 -07:00
return
(key == null ?
me.getKey() == null : key.equals(me.getKey())) &&
(value == null ?
me.getValue()== null : value.equals(me.getValue()));
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String toString() { return key + "=" + value; }
} ///:~