简体   繁体   中英

I am trying to implement my own HashSet and I do not know what iterator() method should return?

I have created an interface MyIterator which has two method declarations hasNext() and next(). MyHashSet implements MyIterator. But i am not able to understand what should i put in the iterator() method?

i want to implement something like this.

MyHashTables hashset = new MyHashTables();

    MyIterator<Object> iterator = hashset.iterator();

    while (iterator.hasNext()){
        System.out.println("Value: "+iterator.next() + " ");  
    }

please help me out!

The iterator() method should return a new object that implements the MyIterator interface. You said you've implemented the next() and hasNext() methods. But where? it should be in a different class than the MyHashTable class. Most likely, you'll want a non-static inner class.

I emphasized the "new" because users of your class would expect to be able to use several iterators at the same time. For example for a double iteration:

MyIterator outerIterator = hashSet.iterator();
while (outerIterator.hasNext) {
    MyIterator innerIterator = hashSet.iterator();
    while (innerIterator.hasNext()) {
        Object o1 = outerIterator.next();
        Object o2 = innerIterator.next();
        // Work with o1 and o2.
    }
}

So your code should look something like:

public class MyHashTable {

    private class HashTableIterator implements MyIterator {

        // HashTableIterator fields

        // HashTableIterator contructor

        public Object next() {
            // Implementation of next()
        }

        public boolean hasNext() {
            // Implementation of hasNext()
        }
    }

    // MyHashTable fields

    // MyHashTable constructor(s)

    // MyHashTable methods

    public MyIterator iterator() {
        return new HashTableIterator();
     }

}

The advantage of a non-static inner class like this is you can use non-static fields of the enclosing class inside the inner class.

I think you want something like this:

Iterface: MyIterator which will have hasNext and next methods as you've described.

Class: MyHashSet

Class: MyHashSetIterator implements MyIterator

Your MyHashTable Class should have its own data structure (DS) -- say an array of objects

MyHashSetIterator should have methods for next() and hasNext() as you've described. It also needs to have a copy of your internal DS and a pointer to a current position in this DS.

The iterator() method in your MyHashTable class returns a copy of this MyHashSetIterator class. Something like this:

Class MyHashSet {

     protected MyHashSetIterator _interator;
     protected Object[] _internalData;

     public MyIterator iterator() {              
           MyIterator result = new MyHashSetIterator(_internalData);                
           return result;
     }

   }

Class MyHashSetIterator implements MyIterator {

           protected Object[] _iteratorData;

           public MyHashSetIterator(Object[] theData) {

               //code to populate _iteratorData with theData

               //code to initialize point to first element of _iteratorData

           }

           public boolean hasNext() { 

                //code to see if pointer has/has not reached end of internal datastructure
           }

           public Object next() { 

                 //code to return Object at element in Object array pointed at by pointer

           }


     }

 }

Keep in mind, synchronizing the iterator with the MyHashSet is very important here. That is, how will you handle the case when you grab the iterator, but then add to the MyHashSet? You will need to decide if MyIterator has a copy of the MyHashSet DS or has pointers to it. This may alter the visibility of your internal variables..

Good luck!

From your question, it's not clear what you're trying to do - so I will guess that MyIterator is an implementation of the JDK's Iterator interface, and MyHashset implements the JDK's Set interface.

If so, you almost surely don't want your hash set to implement iterator directly. Iterators are stateful - they keep track of their current position in a collection of objects which is ordered somehow. So you would implement that separately and return a new one each time iterator() is called.

Here's a simple example of how an iterator is implemented

class ArrayIterator {
   private final Object[] arr;
   private int position = -1;
   ArrayIterator(Object... objs) { this.arr = objs; }
   public boolean hasNext() { return position < arr.length; }
   public Object next () {  return arr[++position]; }
}

If you call iterator() twice on a collection, that needs to return separate instances of Iterator , otherwise the iterator from the first call can be changed by calls on the result of the second call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM