Hash(code and explaination)

来源:互联网 发布:淘宝一伍一拾 编辑:程序博客网 时间:2024/05/29 17:45
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 tableprivate int hashTableSize;private final double MAX_LOAD_FACTOR=0.75;private int tableThreshold;private int modCount=0;//for iterator consistency checksprivate 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 consistencyif(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 bucketi++;while(i<table.length&&(n=table[i])==null)i++;}index=i;next=n;//key pointreturn 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();}@Overridepublic Object[] toArray() {// TODO Auto-generated method stubreturn null;}@Overridepublic <T> T[] toArray(T[] a) {// TODO Auto-generated method stubreturn null;}public boolean add(T item){//compute the  hash table indexint 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 modCountmodCount++;// create a new table entry so its successor is the current head of the listentry=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 tabletable[index]=entry;hashTableSize++;if(hashTableSize>=tableThreshold)rehash(2*table.length+1);//a new entry is addedreturn true;}private void rehash(int newTableSize){//allocate the new hash table and record a reference to the current one in oldTableEntry[] newTable=new Entry[newTableSize];Entry[]oldTable=table;Entry<T> entry;int index;//cycle through the current hash tablefor(int i=0;i<table.length;i++){entry=table[i];//record current entryif(entry!=null){//have at least one element in a linked listdo{//record the next entry in the orginal linked list//nextEntry=entry.next;//compute the new table indexindex=entry.hashValue%newTableSize;entry.next=newTable[index];newTable[index]=entry;entry=entry.next;}while(entry!=null);}}//the table is now newTabletable=newTable;//update the table thresholdtableThreshold=(int)(table.length*MAX_LOAD_FACTOR);//let garbage collection get rid of oldTableoldTable=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 itemwhile(curr!=null){if(curr.value.equals(item)){//we will located item and will remove it,increment modCountmodCount++;//if prev is not null,curr is not the front of the list,just skip over currif(prev!=null){prev.next=curr.next;}else{//curr is the front of the list, the new front of the list is curr.nexttable[index]=curr.next;}//decrement hash table size and return truehashTableSize--;return true;}else{//move prev and curr forwardprev=curr;curr=curr.next;}}return false;}@Overridepublic boolean containsAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean addAll(Collection<? extends T> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean removeAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean retainAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic void clear() {// TODO Auto-generated method stub}}


	
				
		
原创粉丝点击