package practice; import java.util.Collection; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; public class Hash<T> implements Collection<T> { private Entry[] table;//the hash table private int hashTableSize; private final double MAX_LOAD_FACTOR=0.75; private int tableThreshold; private int modCount=0;//for iterator consistency checks private class IteratorImpl implements Iterator<T> { Entry<T> next; int expectedModCount; int index; T lastReturned; public IteratorImpl() { expectedModCount=modCount; int i=0; Entry<T> n=null; if(hashTableSize!=0) { while(i<table.length&&(n=table[i])==null) i++; index=i; next=n; lastReturned=null; } } public boolean hasNext() { return next!=null; } public T next() { //check for iterator consistency if(modCount!=expectedModCount) { throw new ConcurrentModificationException(); } Entry<T> entry=next; if(entry==null) throw new NoSuchElementException(); lastReturned=entry.value; Entry n=entry.next; int i=index; if(n==null) { //we are at the end of a bucket,search for the next non empty bucket i++; while(i<table.length&&(n=table[i])==null) i++; } index=i; next=n;//key point return lastReturned; } public void remove() { if(lastReturned!=null&&modCount==expectedModCount) { Hash.this.remove(lastReturned); expectedModCount=modCount; lastReturned=null; } } } public static void main(String[]args) { Hash<Integer> h=new Hash<Integer>(); h.add(3); h.add(6); h.add(10); Iterator it=h.iterator(); while(it.hasNext()) { System.out.print(it.next()); } } public Hash() { table=new Entry[17]; hashTableSize=0; tableThreshold=(int)(table.length*MAX_LOAD_FACTOR); } public int size() { return hashTableSize; } public boolean isEmpty() { return size()==0; } public boolean contains(Object o) { return false; } public Iterator<T> iterator() { return new IteratorImpl(); } @Override public Object[] toArray() { // TODO Auto-generated method stub return null; } @Override public <T> T[] toArray(T[] a) { // TODO Auto-generated method stub return null; } public boolean add(T item) { //compute the hash table index int hashValue=item.hashCode()&Integer.MAX_VALUE; int index=hashValue%table.length; Entry<T> entry; entry=table[index]; while(entry!=null) { if(entry.value.equals(item)) return false; entry=entry.next; } // we will add the item,so increment modCount modCount++; // create a new table entry so its successor is the current head of the list entry=new Entry<T>(item,hashValue,(Entry<T>)table[index]); //add it at the front of the linked list and increment the size of the hash table table[index]=entry; hashTableSize++; if(hashTableSize>=tableThreshold) rehash(2*table.length+1); //a new entry is added return true; } private void rehash(int newTableSize) { //allocate the new hash table and record a reference to the current one in oldTable Entry[] newTable=new Entry[newTableSize]; Entry[]oldTable=table; Entry<T> entry; int index; //cycle through the current hash table for(int i=0;i<table.length;i++) { entry=table[i];//record current entry if(entry!=null) { //have at least one element in a linked list do { //record the next entry in the orginal linked list //nextEntry=entry.next; //compute the new table index index=entry.hashValue%newTableSize; entry.next=newTable[index]; newTable[index]=entry; entry=entry.next; }while(entry!=null); } } //the table is now newTable table=newTable; //update the table threshold tableThreshold=(int)(table.length*MAX_LOAD_FACTOR); //let garbage collection get rid of oldTable oldTable=null; } public boolean remove(Object item) { int index=(item.hashCode()&Integer.MAX_VALUE)%table.length; Entry<T> curr,prev; curr=table[index]; prev=null; //scan the linked list for item while(curr!=null) { if(curr.value.equals(item)) { //we will located item and will remove it,increment modCount modCount++; //if prev is not null,curr is not the front of the list,just skip over curr if(prev!=null) { prev.next=curr.next; } else { //curr is the front of the list, the new front of the list is curr.next table[index]=curr.next; } //decrement hash table size and return true hashTableSize--; return true; } else { //move prev and curr forward prev=curr; curr=curr.next; } } return false; } @Override public boolean containsAll(Collection<?> c) { // TODO Auto-generated method stub return false; } @Override public boolean addAll(Collection<? extends T> c) { // TODO Auto-generated method stub return false; } @Override public boolean removeAll(Collection<?> c) { // TODO Auto-generated method stub return false; } @Override public boolean retainAll(Collection<?> c) { // TODO Auto-generated method stub return false; } @Override public void clear() { // TODO Auto-generated method stub } }
Hash(code and explaination)
最新推荐文章于 2020-10-08 19:13:38 发布