Java: using iterators in other classes
<p>I'm running into problems trying to put a public iterator in a class to read a Map in the class and implementing this iterator in other classes. Or in other terms,</p>

<p>I have class A. Class A contains a private HashMap that I want to access through a public iterator that iterates this map.</p>

<p>I also have class B. I'm trying to run this iterator in class B to read the contents of class A.</p>

<p>This may seem a bit roundabout (or rather, a lot roundabout), but my assignment specified that the data system in class A be hidden to other classes and suggested using a public iterator to access the data. There is an example of what the method in class B might look like, which I've followed to the best of my ability.</p>

<p>Can't get it to work, though.</p>

<p>Here's a mockup of the code I have. I tried compiling it, and it works exactly as my real code.</p>

<pre><code>// this is class A

public class Giant {

private Map&lt;Item, Integer&gt; myMap = new HashMap&lt;Item, Integer&gt;();

// add a bunch of items to the map, check if they worked fine

public Iterator&lt;Map.Entry&lt;Item, Integer&gt;&gt; giantIterator = myMap.entrySet().iterator();

}

// and this is in class B

public void receive(Giant mouse){
    System.out.println("I've started!");
        Iterator&lt;Map.Entry&lt;Item, Integer&gt;&gt; foo = mouse.giantIterator;

        while (foo.hasNext()) {
            Map.Entry&lt;Item, Integer&gt; entry = foo.next();
            System.out.println("I'm working!");
        }
}
</code></pre>

<p>I also have a testclass which creates objects of either class and then runs the <code>receive</code> method.</p>

<p>I receive the message "I've started!" but not "I'm working!"</p>

<p>At the same time, if I have the two iterators in either class print a <code>toString</code>, the toStrings are identical.</p>

<p>I can't simply move the actions I'm supposed to do in class B to class A either, because the iterator is in several different methods and is used for slightly different things in each.</p>

<p>I'm a bit stumped. Am I missing something from the syntax? Am I importing something wrong? Have I just completely messed up how this is supposed to work? Is this just flat out impossible?</p>
