I was studying about hashset in java and for that I am writing creating my own hashset which will double its size everytimme the threshold value is reached..here I am keeping the threshold as 0.75 of original size . However my code is running into an infinite loop. I tried debugging it but was not able to find my error...

here is the code

package drafta;

import java.util.Iterator;
import java.util.NoSuchElementException;


public class HashSet
{
private Node[] buckets;
private int currentSize;
private int current;

public HashSet(int bucketsLength)
{
 buckets=new Node[bucketsLength];
 currentSize=0;
}

 public boolean contains(Object x)
{
return false;
  // don't implement for the draft
}


 public boolean add(Object x)
 {
  int key=gethashcode(x);
  Node node = buckets[key];
  while(node!=null){
      if(node.data.equals(x)){
          return false;
      }

  }

  if(buckets[current]==null){
  node = new Node(x);
  current=key;
  buckets[key]=node;
  currentSize++;
  }else{
      node = new Node(x);
      node.next=buckets[current];
      current=key;
      buckets[key]=node;
      currentSize++;
  }
  System.out.println("add successful "+ x);
  System.out.println(" size "+currentSize+" rehash "+buckets.length*0.75);

  if(currentSize>(buckets.length*0.75)){
     rehash();
  }
  return true;
 }

private void rehash() {
   Node temp=buckets[current];
   Object s[]=new Object[buckets.length];
   buckets=new Node[2*buckets.length];
currentSize=0;
int i=0;
while(temp!=null){
    s[i]=temp.data;
    temp=temp.next;
    i++;
}
while(i>0){

    add(s[--i]);

}
}


public boolean remove(Object x)
{
return false;
  // don't implement for draft
}

public int gethashcode(Object x){
   int hc = x.hashCode();
   if(hc<0)
       hc=-hc;
   return (hc%buckets.length);
  } 

public Iterator<Object> iterator()
{
   Iterator <Object> i=new HashSetIterator();
return i;
//
 }

 public int size()
 {
return currentSize;
  //
 }

 private void resize(int newLength)
 {
    }

  public int getlength()
 {
return buckets.length;
  //
  }


 class Node
{

    public Object data;
    public Node next;
    public Node(Object x) {
        data=x;
    }
    public String toString(){
        return data.toString();
    }
}

  class HashSetIterator implements Iterator<Object>
 {
  private int bucket=0;
  private Node currentnode;

   public HashSetIterator()
   {
    currentnode=buckets[current];
  }

  public boolean hasNext()
  {
    if(currentnode.next!=null)
        return true;
    else 
        return false;
     //
  }

  public Object next()
  {
    return currentnode.next;
     //
  }

@Override
public void remove() {
    currentnode.next=currentnode.next.next;

}



   }
}

this is the main class which I am using to test my code

package drafta;

import java.util.Iterator;

public class HashSetTester
{
public static void main(String[] args)
{
  HashSet names = new HashSet(5);

  names.add("Harry");
  names.add("Sue");
  names.add("Nina");

  System.out.println(names.size() + " " + names.getlength());


  names.add("Susannah");
  System.out.println(names.size() + " " + names.getlength());



  System.out.println();
  names.add("Larry");
  names.add("Juliet");
  names.add("Katherine");
  names.add("Romeo");
  names.add("Maria");


  System.out.println(names.size() + " " + names.getlength());

  names.add("Ann");
  names.add("Taylor");
  System.out.println(names.size() + " " + names.getlength());




}
}

can someone please point out my mistake..the code is going into infintie loop when it calls rehash for second time..first time it goes through correctly...

有帮助吗?

解决方案

You arn't changing any conditions in your while loop in the add method - so there is no reason for it to break out.

while(node!=null){
      if(node.data.equals(x)){
          return false;
      }
  }

You will continue looping until the node is null (which never gets set) or the node data ever equals x, but the data value also never gets set.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top