Klaabu
Klaabu

Reputation: 345

Java: using iterators in other classes

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,

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

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

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.

Can't get it to work, though.

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

// this is class A

public class Giant {

private Map<Item, Integer> myMap = new HashMap<Item, Integer>();

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

public Iterator<Map.Entry<Item, Integer>> giantIterator = myMap.entrySet().iterator();

}

// and this is in class B

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

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

I also have a testclass which creates objects of either class and then runs the receive method.

I receive the message "I've started!" but not "I'm working!"

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

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.

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?

Upvotes: 1

Views: 87

Answers (3)

Steve
Steve

Reputation: 1489

If you have one iterator, and you want various classes to access it, then set that variable to "static".

Upvotes: 0

user949300
user949300

Reputation: 15729

Your iterator is constructed when A is constructed, and at that time the Map is empty.

Make a method getIterator() that returns an up to date version.

Upvotes: 2

EJK
EJK

Reputation: 12527

Try exposing the iterator through a function like this:

public class Giant {
    private Map<Item, Integer> myMap = new HashMap<Item, Integer>();
    public Iterator<Map.Entry<Item, Integer>> getGiantIterator() {
         return myMap.entrySet().iterator();
    }
}

And in class B change:

    Iterator<Map.Entry<Item, Integer>> foo = mouse.giantIterator;

to

Iterator<Map.Entry<Item, Integer>> foo = mouse.getGiantIterator();

That way the iterator will not be created until it is needed.

The way you have coded it, the iterator is created while the map is still empty. I suspect this may be the root of your problem.

Upvotes: 3

Related Questions