/**
为了把Set扩展成Map,可以考虑新增一个SimpleEntry类,该类代表一个
key-value对,当Set集合的集合元素都是SimpleEntry对象时,该Set集合就能被当成Map使用
*/
class SimpleEntry<k , v>
implements Map.Entrys<k , v>, java.io.Serializable
{
private final K key;
private V value;
//定义构造器
public SimpleEntry(K key, V value)
{
this.key = key;
this.value = value;
}
public SimpleEntry(Map.Entry<? extends K, ? extends V> entry)
{
this.key = entry.getKey();
this.value = entry.getValue();
}
//获取key
public K getKey()
{
return key;
}
//获取value
public V getValue()
{
return value;
}
//改变key-value对的value值
public V setValue(V value)
{
V oldValue = this.value;
this.value = value;
return oldValue;
}
//根据key比较两个SimpleEntry是否相等
public boolean equals(Object o)
{
if( o == this)
{
return true;
}
if(o.getClass() == SimpleEntry.class)
{
SimpleEntry se = (SimpleEntry)o;
return se.getKey().equls(getKey());
}
}
//根据key计算hashCode
public int hashCode()
{
return key == null ? 0 : key.hashCode();
}
public String toString()
{
return key + "=" + value;
}
}
/**
继承HashSet实现一个Map
*/
public class Set2Map<K, V>
extends HashSet<SimpleEntry<K, V>>
{
//实现清空所有key-value
public void clear()
{
super.clear();
}
//判断是否包含某个key
public boolean containsKey(Object key)
{
return super.contains(new SimpleEntry<K , V>(key,null));
}
//判断是否包含某个value
public boolean containsValue(Object value)
{
for(SimpleEntry<K , V> se : this)
{
if(se.getValue().equals(value)) return true;
}
}
//根据指定的key取出对应的value
public V get(Object key)
{
for(SimpleEntry<K , V> se:this)
{
if(se.getKey().equals(key)) return se.getValue();
}
}
//将指定key-value放入集合中
public V put(K key, V value)
{
add(new SimpleEntry<K , V>(key,value));
return value;
}
//将另一个Map的key-value对放入该Map中
public void putAll(Map<? extends K, ? extends V> m)
{
for(K key : m.keySet())
{
add(new SimpleEntry<K , V>(key , m.get(key)));
}
}
//根据指定key删除指定key-value对
public V removeEntry(Object key)
{
for(Iterator<SimpleEntry<K , V>> it = this.iterator();
it.next() ; )
{
SimpleEntry<K , V> en = (SimpleEntry<K , V>)it.next();
if(en.getKey().equals(key))
{
V v = en.getValue();
it.remove();
return v;
}
}
return null;
}
//获取该Map中包含多少个key-value对
public int size()
{
return super.size();
}
}
HashMap中的key单提出来,就是一个HashSet集合。从set的角度来说,map中的每一个元素都可以放到set集合中,只不过每一个元素都有一个附属value。
从Map的角度来说呢,set中的每一个元素都是一个value为空的map.Entry。