HashTable扩展:一个键对应多个值

基于有时候想存储一对多的数据结构的需求,自己写了个类来存储。

package javax.util.collection.collections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 此类是用来存储键值对数据的,但一个键可以映射多个值。
*
* @author Administrator
*/
public class O2NMap<K, V> implements Map<K, V> {
private Map<Key<K>, V> myMap;

public O2NMap() {
myMap = new Hashtable<Key<K>, V>();
}

public O2NMap(int initialCapacity) {
myMap = new Hashtable<Key<K>, V>(initialCapacity);
}

public O2NMap(int initialCapacity, float loadFactor) {
myMap = new Hashtable<Key<K>, V>(initialCapacity, loadFactor);
}

public O2NMap(Map<? extends Key<K>, ? extends V> t) {
myMap = new Hashtable<Key<K>, V>(t);
}

@Override
public synchronized void clear() {
myMap.clear();
}

@Override
public synchronized boolean containsKey(Object key) {
for (K k : keySet()) {
if (k == key || k.equals(key)) {
return true;
}
}
return false;
}

@Override
public boolean containsValue(Object value) {
if (value == null)
throw new NullPointerException();

for (V v : values()) {
if (v == value || v.equals(value)) {
return true;
}
}
return false;
}

@Override
public synchronized Set<java.util.Map.Entry<K, V>> entrySet() {
Set<Entry<K, V>> entrySets = new HashSet<Entry<K, V>>();

Set<Entry<Key<K>, V>> entrySet = myMap.entrySet();
for (Iterator<Entry<Key<K>, V>> it = entrySet.iterator(); it.hasNext();) {
Entry<Key<K>, V> entry = it.next();
Key<K> key = entry.getKey();
V value = entry.getValue();
entrySets.add(new MyEntry(key.value(), value));
}
return entrySets;
}

@Deprecated
@Override
public synchronized V get(Object key) {
List<V> vList = new ArrayList<V>();

Set<Entry<K, V>> entrySet = entrySet();
for (Entry<K, V> entry : entrySet) {
K k = entry.getKey();
if (k == key || k.equals(key)) {
vList.add(entry.getValue());
}
}
return vList.size() == 1 ? vList.get(0) : vList.get(vList.size() - 1);
}

public synchronized List<V> getAll(Object key) {
List<V> vList = new ArrayList<V>();

Set<Entry<K, V>> entrySet = entrySet();
for (Entry<K, V> entry : entrySet) {
K k = entry.getKey();
if (k == key || k.equals(key)) {
vList.add(entry.getValue());
}
}
return vList;
}

@Override
public synchronized boolean isEmpty() {
return size() == 0;
}

@Override
public synchronized Set<K> keySet() {
Set<K> kSet = new HashSet<K>();

Set<Key<K>> keySet = myMap.keySet();
for (Iterator<Key<K>> it = keySet.iterator(); it.hasNext();) {
Key<K> key = it.next();
kSet.add(key.value());
}
return kSet;
}

@Override
public synchronized V put(K key, V value) {
if (value == null)
throw new NullPointerException();
Set<Entry<K, V>> set = entrySet();
if (set.size() == 0) {
myMap.put(new Key<K>(key), value);
} else {
boolean isExisted = false;
for (Entry<K, V> entry : set) {
K k = entry.getKey();
V v = entry.getValue();

if ((k == key || k.equals(key))
&& (v == value || v.equals(value)))
isExisted = true;
}
if (!isExisted)
myMap.put(new Key<K>(key), value);
}
return value;
}

@Override
public synchronized void putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}

@Override
public synchronized V remove(Object key) {
Map<Key<K>, V> temp = new Hashtable<Key<K>, V>();
V v = null;
Set<Entry<K, V>> set = entrySet();
for (Entry<K, V> entry : set) {
if (entry != null) {
K k = entry.getKey();
if ((k == key) || (k.equals(key))) {
v = entry.getValue();
} else {
temp.put(new Key<K>(k), entry.getValue());
}
}
}
myMap = temp;
return v;
}

@Override
public synchronized int size() {
return myMap.size();
}

@Override
public synchronized Collection<V> values() {
Collection<V> collection = new ArrayList<V>();
for (Entry<K, V> entry : entrySet()) {
collection.add(entry.getValue());
}
return collection;
}

/**
* Map.Entry接口的实现类
*
* @author Administrator
*
*/
private class MyEntry implements Map.Entry<K, V> {
private K k;
private V v;

public MyEntry(K k, V v) {
this.k = k;
this.v = v;
}

@Override
public K getKey() {
return k;
}

@Override
public V getValue() {
return v;
}

@Override
public V setValue(V value) {
if (value == null)
throw new NullPointerException();

V oldValue = this.v;
this.v = value;
return oldValue;
}
}

/**
* 用Key类代替泛型中的K
*
* @author Administrator
*
* @param <K>
*/
private class Key<K> implements Serializable {
private static final long serialVersionUID = 1753979936226694189L;
private K value;

public K value() {
return value;
}

public Key(K value) {
this.value = value;
}

@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (obj instanceof Key) {
Key<K> k = (Key<K>) obj;
if (value.equals(k.value())) {
return true;
}
}
return false;
}

@Override
public String toString() {
return value.toString();
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值