Question

We have to design our own HashSet class. (not using the JAVA api).

I implemented it, but I have a problem with printing it.

I think there is a problem with the iteration constructor. any advice?

Hash set:

package CountWords;

import java.util.Iterator;

public class HashWordSet implements WordSet  {

    private int size = 0;                    
    private Node[] buckets = new Node[8];    

    public class Node { 
    Word value;
    Node next = null;

    public Node (Word w) { value = w; }
    }

    private class HashWordIterator implements Iterator<Word> {

        private int pos, index = 0;
        private Word[] words = new Word[size];
        private Node node;

        public HashWordIterator()
        {
            for(int i=0; i<words.length;i++){
            words[index++]=node.value;
            node = node.next;
            }
        }

        public boolean hasNext() {
            if (size > pos)//root != null && root.next != null)
                return true;

            return false;
        }

        public Word next() {
            return words[pos++];
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void add(Word word) {
        int pos = (word.hashCode() % buckets.length);        
        Node node = buckets[pos];

        while (node != null) {
            if (node.value.equals(word))
                return;
            else
            node = node.next;
        }

        node = new Node(word);
        node.next = buckets[pos];
        size++;    
        if (size == buckets.length)
            rehash();
        }    
    }

    private void rehash() {
        Node[] tmp = buckets;
        buckets = new Node[2*tmp.length];                    
        size = 0;        

        for (Node n : tmp) {
            if (n == null) continue;
            while (n != null) {
                add(n.value);
                n = n.next;
            }
        }
    }

    @Override
    public boolean contains(Word word) {
        int pos = (word.hashCode() % buckets.length); //
        Node n = buckets[pos];

        while (n != null) {
            if (n.value.equals(word))
                return true;
            else
                n = n.next; // stega fram i listan
        }
        return false;
    }

    public String toString () {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < buckets.length; i++) {
            sb.append(buckets[i].value.toString());
        }

        return sb.toString();
    }

    @Override
    public Iterator<Word> iterator() {
        return new HashWordIterator() ;
    } 
}

Main:

package CountWords;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;


public class WordCount2Main {

    public static void main(String[] args) {
        File file = new File("/Users/sa/Documents/workspace/1DV007/src/CountWords/Words.txt");

        HashWordSet  Hset = new HashWordSet ();
        //TreeSet<Object> Tset = new TreeSet<Object>();
        TreeWordSet Tset = new TreeWordSet();
        Scanner Scan;
        int i=0;
        try {
            Scan = new Scanner(file);

            while (Scan.hasNext()) {
                String text = Scan.next();
                Word Y=new Word(text);
                Hset.add(Y);
                i++;
            }

            System.out.println("TreeSet: " + "\n");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println();
        Iterator<Word> iter2 = Hset.iterator();

        while(iter2.hasNext()) {
            Object o2 = iter2.next();
            System.out.print(o2 + " ");
        }

         System.out.println(Hset.size() + " Hash set:: "+Hset );
    }
}
Was it helpful?

Solution

In your constructor I see

words[index++]=node.value

You don't seem to instantiate node anywhere

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top