Java Hashtable链接

时间:2013-12-03 06:23:13

标签: java hashtable hashcode

我应该编写一个Java Hashtable函数,可以导入java.util.Iterator,java.util.NoSuchElementException和java.util.ConcurrentModificationException。没有具体说明要使用哪种类型的哈希函数。那我怎么写我的getHashCode函数。到目前为止我的代码:

package data_structures;

import java.util.ConcurrentModificationException;
import java.util.Iterator;

import data_structures.LinkedListDS.IteratorHelper;

public class Hashtable<K,V> implements DictionaryADT<K,V> {

private int currentSize,maxSize,tableSize;
private long modCounter;
private LinkedListDS <DictionaryNode<K,V>> [] list;

class DictionaryNode<K,V> implements Comparable<DictionaryNode<K,V>> {
    K key;
    V value;

    public DictionaryNode(K k, V v) {
        key = k;
        value = v;
    }

    public int compareTo(DictionaryNode<K,V> node) {
        return ((Comparable<K>)key).compareTo((K)node.key);
    }
}

public Hashtable(int n) {
    currentSize = 0;
    maxSize = n;
    modCounter = 0;
    tableSize = (int) (maxSize * 1.3f);
    list = new LinkedListDS[tableSize];
    for (int i=0; i < tableSize; i++)
        list[i] = new LinkedListDS<DictionaryNode<K,V>>();
}

public boolean contains(K key) {
    return list[getHashCode(key)].contains(new DictionaryNode<K,V>(key,null));
}

private int getHashCode(K key) {
    // TODO Auto-generated method stub
    return 0;
}

public boolean insert(K key, V value) {
    if (isFull())
        return false;
    if (list[getHashCode(key)].contains(new DictionaryNode<K,V>(key,null)))
        return false;
    list[getHashCode(key)].addLast(new DictionaryNode<K,V>(key,value));
    currentSize++;
    modCounter++;
    return true;
}

@Override
public boolean remove(K key) {
    if (isEmpty())
        return false;
    if (!list[getHashCode(key)].contains(new DictionaryNode<K,V>(key,null)))
        return false;
    list[getHashCode(key)].remove(new DictionaryNode<K,V>(key,null));
    currentSize--;
    modCounter++;
    return true;
}

public V getValue(K key) {
    DictionaryNode<K,V> tmp = list[getHashCode(key)].find(new DictionaryNode<K,V>(key,null));
    if (tmp == null)
        return null;
    return tmp.value;
}

@Override
public K getKey(V value) {
    DictionaryNode<K,V> tmp = list[getHashCode(value)].find(new DictionaryNode<K,V>(null,value));
    if (tmp == null)
        return null;
    return tmp.key;
}

public int size() {
    return currentSize;
}

@Override
public boolean isFull() {
    return currentSize == maxSize;
}

public boolean isEmpty() {
    return currentSize == 0;
}

@Override
public void clear() {
    currentSize = 0;
    modCounter++;
    for (int i=0; i<tableSize; i++)
        list[i].makeEmpty();
}

public Iterator<K> keys() {
    return new KeyIteratorHelper();
}

public Iterator<V> values() {
    return new ValueIteratorHelper();
}

abstract class IteratorHelper<E> implements Iterator<E> {
    protected DictionaryNode<K,V> [] nodes;
    protected int idx;
    protected long modCheck;

    public IteratorHelper() {
        nodes = new DictionaryNode[currentSize];
        idx = 0;
        int j = 0;
        for (int i=0; i < tableSize; i++)
            for (DictionaryNode n : list[i])
                nodes[j++] = n;
        nodes = (DictionaryNode<K,V>[]) ObjectSorter.quickSort(nodes);
    }

    public boolean hasNext() {
        if (modCheck != modCounter)
            throw new ConcurrentModificationException();
        return idx < currentSize;
    }

    public abstract E next();

    public void remove() {
        throw new UnsupportedOperationException();
    }
}

class KeyIteratorHelper<K> extends IteratorHelper<K> {
    public KeyIteratorHelper() {
        super();
    }

    public K next() {
        return (K) nodes[idx++].key;
    }
}

class ValueIteratorHelper<V> extends IteratorHelper<V> {
    public ValueIteratorHelper() {
        super();
    }

    public V next() {
        return (V) nodes[idx++].value;
    }
}

}

0 个答案:

没有答案