Hash类的实现

package Hash;
import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;
import MyInterface.*;

/**
 * Hash类的实现
 */
public class Hash<T> implements Collection<T> {

	// ----------------------------------------------------------------
	// 结点类
	private static class Entry<T> {
		T value;
		int hashValue;
		Entry<T> next;

		public Entry(T value, int hashValue, Entry<T> next) {
			this.value = value;
			this.hashValue = hashValue;
			this.next = next;
		}
	}

	// ----------------------------------------------------------------
	// Hash类实例变量
	private Entry[] table;
	private int hashTableSize;
	private final double MAX_LOAD_FACTOR = 0.75;
	private int tableThreshold;
	private int modCount = 0;

	// Hash类构造函数
	public Hash() {
		table = new Entry[17];
		hashTableSize = 0;
		tableThreshold = (int) (table.length * MAX_LOAD_FACTOR);
	}

	// ----------------------------------------------------------------
	// add()方法
	public boolean add(T item) {
		int hashValue = item.hashCode() & Integer.MAX_VALUE, index = hashValue
				% table.length;
		Entry<T> entry;
		entry = table[index];

		while (entry != null) {
			if (entry.value.equals(item))
				return false;
			entry = entry.next;
		}
		modCount++;
		entry = new Entry<T>(item, hashValue, (Entry<T>) table[index]);
		table[index] = entry;
		hashTableSize++;
		if (hashTableSize >= tableThreshold)
			rehash(2 * table.length + 1);
		return true;
	}

	// 散列表的再散列
	private void rehash(int newTableSize) {
		Entry[] newTable = new Entry[newTableSize], oldTable = table;
		Entry<T> entry, nextEntry;
		int index;

		for (int i = 0; i < table.length; i++) {
			entry = table[i];
			if (entry != null) {
				table[i] = null;
				do {
					nextEntry = entry.next;
					index = entry.hashValue % newTableSize;
					entry.next = newTable[index];
					newTable[index] = entry;
					entry = nextEntry;
				} while (entry != null);
			}
		}
		table = newTable;
		tableThreshold = (int) (table.length * MAX_LOAD_FACTOR);
		oldTable = null;
	}

	// remove()方法
	public boolean remove(Object item) {
		int index = ((item.hashCode() & Integer.MAX_VALUE)) % table.length;
		Entry<T> curr, prev;
		curr = table[index];
		prev = null;
		while (curr != null) {
			if (curr.value.equals(item)) {
				modCount++;
				if (prev != null)
					prev.next = curr.next;
				else
					table[index] = curr.next;
				hashTableSize--;
				return true;

			} else {
				prev = curr;
				curr = curr.next;
			}
		}
		return false;
	}

	public void clear() {
		int len = table.length;
		for (int i=0;i < len; i++)
			table[i] = null;
		modCount++;
		hashTableSize = 0;
	}

	public boolean contains(Object item) {
		int index = (item.hashCode() & Integer.MAX_VALUE) % table.length;
		Entry<T> entry;
		entry = table[index];
		while (entry != null) {
			if (entry.value.equals(item))
				return true;
			entry = entry.next;
		}
		return false;
	}

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

	public Iterator<T> iterator() {
		return new MyIterator();
	}

	public int size() {
		return hashTableSize;
	}

	public Object[] toArray() {
		Object[] arr = new Object[hashTableSize];
		Iterator<T> iter = iterator();
		for(int i = 0; iter.hasNext(); i++)
			arr[i] = iter.next();
		return arr;
	}
	
	public String toString() {
		int max = hashTableSize - 1;
		StringBuffer buf = new StringBuffer();
		Iterator<T> iter = iterator();

		buf.append("[");
		for (int i = 0; i <= max; i++)
		{
			buf.append(iter.next());

	    	if (i < max)
				buf.append(", ");
		}
		buf.append("]");
		return buf.toString();
	}
	
	private class MyIterator implements Iterator<T> {
		
		Entry<T> next;
		int expectedModCount;
		int index;
		T lastReturned;
		
		//构造函数
		MyIterator() {
			int i = 0;
			Entry<T> n = null;
			expectedModCount = modCount;
			if(hashTableSize != 0) {
				while(i < table.length && ((n = table[i]) == null))
					i++;
			}
			next = n;
			index = i;
			lastReturned = null;
		}

		public boolean hasNext() {
			return next != null;
		}

		public T next() {
			if (modCount != expectedModCount)
				 throw new ConcurrentModificationException();

			Entry<T> entry = next;

			if (entry == null)
				 throw new NoSuchElementException();
			lastReturned = entry.value;
			Entry<T> n = entry.next;
			int i = index;
			if (n == null) {
				i++;
				while (i < table.length && ((n = table[i]) == null))
					i++;
			}
			index = i;
			next = n;
			return lastReturned;
		}

		public void remove() {
			 // check for a missing call to next() or previous()
			   if (lastReturned == null)
			      throw new IllegalStateException(
			         "Iterator call to next() " +
			         "required before calling remove()");
				if (modCount != expectedModCount)
					 throw new ConcurrentModificationException();

				// remove lastReturned by calling remove() in Hash.
				// this call will increment modCount
				Hash.this.remove(lastReturned);
				expectedModCount = modCount;
				lastReturned = null;
		}
		
	}
	
}

你可能感兴趣的:(hash)