哪位大侠能否帮我分析一下Hashtable的源码 哪位大侠能否帮我分析一下Hashtable的源码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以先分析一下啊HashMap的源码,Hashtable还稍微麻烦一些!我这有个简单版本的Map,但还不是HashMap,HashMap就是利用散列码存取数据了,你自己再看一下了,先看一下这个简单的!慢慢来。代码不是很标准,但基本原理就是这样package com.gl.test;import java.util.AbstractSet;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class MyMap<T, U> { /** * @author Administrator Map中的内部类Entry,表示键值对 * @param <T> * @param <U> */ public static class Entry<T, U> { private T key; private U value; Entry(T key, U value) { this.key = key; this.value = value; } Entry() { } public T getKey() { return key; } public void setKey(T key) { this.key = key; } public U getValue() { return value; } public void setValue(U value) { this.value = value; } public int hashCode(){ return key.hashCode() + value.hashCode(); } public boolean equals(Object obj){ if(obj instanceof Entry && ((Entry<T,U>)obj).key.equals(this.key) && ((Entry<T,U>)obj).value.equals(this.value)){ return true; } return false; } public String toString(){ return this.key + "=>" + this.value; } } private int capacity = 20; private int index = 0; private Entry<T, U>[] entries = new Entry[20]; private void alloc() { Entry<T, U>[] entTemp = new Entry[this.capacity]; for (int i = 0; i < this.entries.length; i++) { entTemp[i] = this.entries[i]; this.entries[i] = null; } this.entries = entTemp; } /** * 存入键值对 * * @param key * @param value */ public void put(T key, U value) { if (null == key) { throw new RuntimeException("key不能为null"); } if(null != this.getEntry(key)){ this.getEntry(key).value = value; } entries[index++] = new Entry<T, U>(key, value); if (this.index >= this.capacity) { this.capacity *= 2; this.alloc(); } } /** * 根据key得到键值对 * * @param key * @return 值 */ private Entry<T, U> getEntry(T key) { if (null == key) { throw new RuntimeException("key不能为null"); } for (Entry<T, U> e : this.entries) { if (null == e) { break; } if (e.key.equals(key)) { return e; } } return null; } /** * 根据key得到值 * @param key * @return */ public U get(T key){ Entry<T,U> entry = this.getEntry(key); if(null != entry){ return entry.value; } return null; } /** * 返回Map中键值对的个数 * @return */ public int length(){ return this.index; } /** * 返回Map的容量 * @return 容量 */ public int capacity(){ return this.capacity; } /** * 此Map映射是否为空 * @return */ public boolean empty(){ return this.index == 0; } /** * 返回键值对set * @return */ public Set<Entry<T,U>> entrySet(){ return new AbstractSet<Entry<T,U>>(){ private int setIndex = 0; @Override public Iterator<Entry<T, U>> iterator() { return new Iterator<Entry<T,U>>(){ public boolean hasNext() { return setIndex < index; } public Entry<T, U> next() { return entries[setIndex++]; } public void remove() { throw new UnsupportedOperationException(); } }; } @Override public int size() { return index; } }; } /** * 返回键set * @return */ public Set<T> keySet(){ return new AbstractSet<T>(){ private int setIndex = 0; @Override public Iterator<T> iterator() { return new Iterator<T>(){ public boolean hasNext() { return setIndex < index; } public T next() { return entries[setIndex++].key; } public void remove() { throw new UnsupportedOperationException(); } }; } @Override public int size() { return index; } }; } public static void main(String[] args){ MyMap<String,String> map = new MyMap<String,String>(); map.put("1", "234"); for(int i=0; i<100; i++){ map.put(String.valueOf(i), "w"+i); } }} 看下它的源码和JAVADOC就都明白了 file问题 类中有String, File类成员变量,如何进行clone 急切求助! 高手请进 ■■■ 在线等,求一个java的课程设计下载(航空公司定票系统)■■■ 求助!!关于两行代码的理解问题!请大虾近来看看!!(在线等待) 求教前辈们一个关于finalize()的问题,谢谢大家帮助:) 我的问题啊~!真的没有人愿意帮助我吗?~~~~~~~~~~~~~~~~~~~~~5555 JBuilder的使用问题 请大家来帮帮忙.. 关于图片显示(在线等急) applet在MyEclipse内正常运行,但在html内不能自动更新内容
我这有个简单版本的Map,但还不是HashMap,HashMap就是利用散列码存取数据了,你自己再看一下了,先看一下这个简单的!慢慢来。代码不是很标准,但基本原理就是这样package com.gl.test;
import java.util.AbstractSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;public class MyMap<T, U> {
/**
* @author Administrator Map中的内部类Entry,表示键值对
* @param <T>
* @param <U>
*/
public static class Entry<T, U> {
private T key;
private U value; Entry(T key, U value) {
this.key = key;
this.value = value;
} Entry() {
} public T getKey() {
return key;
} public void setKey(T key) {
this.key = key;
} public U getValue() {
return value;
} public void setValue(U value) {
this.value = value;
}
public int hashCode(){
return key.hashCode() + value.hashCode();
}
public boolean equals(Object obj){
if(obj instanceof Entry && ((Entry<T,U>)obj).key.equals(this.key) && ((Entry<T,U>)obj).value.equals(this.value)){
return true;
}
return false;
}
public String toString(){
return this.key + "=>" + this.value;
}
} private int capacity = 20;
private int index = 0;
private Entry<T, U>[] entries = new Entry[20]; private void alloc() {
Entry<T, U>[] entTemp = new Entry[this.capacity];
for (int i = 0; i < this.entries.length; i++) {
entTemp[i] = this.entries[i];
this.entries[i] = null;
}
this.entries = entTemp;
} /**
* 存入键值对
*
* @param key
* @param value
*/
public void put(T key, U value) {
if (null == key) {
throw new RuntimeException("key不能为null");
}
if(null != this.getEntry(key)){
this.getEntry(key).value = value;
}
entries[index++] = new Entry<T, U>(key, value);
if (this.index >= this.capacity) {
this.capacity *= 2;
this.alloc();
} } /**
* 根据key得到键值对
*
* @param key
* @return 值
*/
private Entry<T, U> getEntry(T key) {
if (null == key) {
throw new RuntimeException("key不能为null");
}
for (Entry<T, U> e : this.entries) {
if (null == e) {
break;
}
if (e.key.equals(key)) {
return e;
}
}
return null;
}
/**
* 根据key得到值
* @param key
* @return
*/
public U get(T key){
Entry<T,U> entry = this.getEntry(key);
if(null != entry){
return entry.value;
}
return null;
}
/**
* 返回Map中键值对的个数
* @return
*/
public int length(){
return this.index;
}
/**
* 返回Map的容量
* @return 容量
*/
public int capacity(){
return this.capacity;
}
/**
* 此Map映射是否为空
* @return
*/
public boolean empty(){
return this.index == 0;
}
/**
* 返回键值对set
* @return
*/
public Set<Entry<T,U>> entrySet(){
return new AbstractSet<Entry<T,U>>(){
private int setIndex = 0;
@Override
public Iterator<Entry<T, U>> iterator() {
return new Iterator<Entry<T,U>>(){ public boolean hasNext() {
return setIndex < index;
} public Entry<T, U> next() {
return entries[setIndex++];
} public void remove() {
throw new UnsupportedOperationException();
}
};
} @Override
public int size() {
return index;
}
};
}
/**
* 返回键set
* @return
*/
public Set<T> keySet(){
return new AbstractSet<T>(){
private int setIndex = 0;
@Override
public Iterator<T> iterator() {
return new Iterator<T>(){ public boolean hasNext() {
return setIndex < index;
} public T next() {
return entries[setIndex++].key;
} public void remove() {
throw new UnsupportedOperationException();
}
};
} @Override
public int size() {
return index;
}
};
}
public static void main(String[] args){
MyMap<String,String> map = new MyMap<String,String>();
map.put("1", "234");
for(int i=0; i<100; i++){
map.put(String.valueOf(i), "w"+i);
}
}}