java map散列 493

可以保存与读取键值的类,

package test;


import java.util.Map;


public class MapEntry<K, V> implements Map.Entry<K,V>{
private K key;
private V value;



public MapEntry(K key, V value) {
super();
this.key = key;
this.value = value;
}


@Override
public K getKey() {
// TODO Auto-generated method stub
return key;
}


@Override
public V getValue() {
// TODO Auto-generated method stub
return value;
}


@Override
public V setValue(V v) {
// TODO Auto-generated method stub
V result = value;
value = v;
return result;
}

public int hashCode() {
return (key == null ? 0:key.hashCode()) ^ (value == null ? 0:value.hashCode());
}

@SuppressWarnings("rawtypes")
public boolean equals(Object o) {
if(!(o instanceof MapEntry))
return false;
MapEntry me = (MapEntry)o;
return (key == null ? 
me.getKey() == null : key.equals(me.getValue())) &&
  (value == null ? 
me.getValue() == null : value.equals(me.getValue()));
}

public String toString() {
return key + "=" +value;
}


}


实现简单的散列Map,

package test;


import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;


/**
 * @author chenw
 * 实现简单的散列Map
 */
class Main<K,V> extends AbstractMap<K,V> {
static final int SIZE = 997;
@SuppressWarnings("unchecked")

LinkedList<MapEntry<K,V>>[] buckets = new LinkedList[SIZE];
public V put(K key,V value) {
V oldValue = null;
int index = Math.abs(key.hashCode())%SIZE;
if(buckets[index] == null) {
buckets[index] = new LinkedList<MapEntry<K,V>>();
}

//bucket是实际散列表的数组,为了使散列均匀分布,其大小一般为质数
LinkedList<MapEntry<K,V>> bucket = buckets[index];
MapEntry<K, V> pair = new MapEntry<K,V>(key,value);
boolean found = false;
ListIterator<MapEntry<K, V>> it = bucket.listIterator();
while(it.hasNext()) {
MapEntry<K, V> ipair = it.next();
if(ipair.getKey().equals(key)) {
oldValue = ipair.getValue();
it.set(pair);
found = true;
break;
}
}
if(!found) {

//新元素添加到list末尾的某个特定的bucket中
buckets[index].add(pair);}
return oldValue;

}

public V get(Object key) {
int index = Math.abs(key.hashCode())%SIZE;
if(buckets[index] == null)
return null;
for(MapEntry<K, V> ipair : buckets[index]) {
if(ipair.getKey().equals(key)) {
return ipair.getValue();
}
}
return null;

}

@Override
public Set<Map.Entry<K, V>> entrySet() {
// TODO Auto-generated method stub
Set<Map.Entry<K, V>> set = new HashSet<Map.Entry<K, V>>();
for(LinkedList<MapEntry<K, V>> bucket : buckets) {
if(bucket == null) 
continue;
for(MapEntry<K, V> mpair :bucket) {
set.add(mpair);
}
}
return set;
}

public static void main(String[] args) {
Main<String ,String> m = new Main<String ,String>();
HashMap<String, String> map2 = new HashMap<String, String>(); 
map2.put("2", "B"); 
map2.put("3", "C"); 
m.putAll(map2);
System.out.println(m);
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值