哪位大侠能否帮我分析一下Hashtable的源码

解决方案 »

  1.   

    你可以先分析一下啊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);
        }
       
    }}
      

  2.   

    看下它的源码和JAVADOC就都明白了