5

I am attempting to write a program that iterates over a set of sets. In the example code below, I am getting an error that stating that iter.next() is of type object rather than a set of strings. I am having some other more mysterious issues with iterating over sets of sets as well. Any suggestions?

 Set<String> dogs= new HashSet<String>(); dogs.add("Irish Setter"); dogs.add("Poodle"); dogs.add("Pug"); dogs.add("Beagle"); Set<String> cats = new HashSet<String>(); cats.add("Himalayan"); cats.add("Persian"); Set<Set<String>> allAnimals = new HashSet<Set<String>>(); allAnimals.add(cats); allAnimals.add(dogs); Iterator iter = allAnimals.iterator(); System.out.println(allAnimals.size()); while (iter.hasNext()) { System.out.println(iter.next().size()); } 

A related question with the same setup (minus the loop).
The code fragment below results in a final output that includes tildes. But I don't want to change allAnimals as I go! How can I edit extension without affecting the larger set (allAnimals).

 for (Set<String> extension : allAnimals) { System.out.println("Set size: " + extension.size()); extension.add("~"); System.out.println(extension); } System.out.println(allAnimals); 
2
  • 3
    You don't need to call someSet.clear(); right after creating a set. Commented Jul 26, 2013 at 14:22
  • Thanks! I'm nervous about not initiating variables so I go overboard sometimes. Commented Jul 26, 2013 at 18:32

4 Answers 4

6

Your allAnimals variable is of type Set<Set<String>>, however, when you ask its Iterator you "forget" the type information. According to the compiler, your iterator just contains Objects. Change the line where you get the Iterator to this

Iterator<Set<String>> iter = allAnimals.iterator(); 

and all should be fine.

Sign up to request clarification or add additional context in comments.

Comments

4

Use an enhanced for loop for traversing the sets, is easier than using an iterator:

for (Set<String> names : allAnimals) { System.out.println(names.size()); } 

For example, to traverse all the animal's names:

for (Set<String> names : allAnimals) { for (String name : names) { System.out.println(name); } } 

3 Comments

@user2623054 This is good advice, I recommend following it. However, I would also recommend marking mthmulders answer as the accepted one, as it precisely identifies the problem.
This method seems to result in a new error: "Can only iterate over an array or an instance of java.lang.Iterable"
Set is an instance of Iterable. You must be doing something wrong, because the above works, and it's the recommended way to iterate nowadays.
2

You do not mention the type on which your iterator is defined. So as far as it is concerned it expects an object as next.

Comments

1

I would just use a (nested) foreach loop:

for(Set<String> animals : allAnimals) { int size = animals.size(); // if you want it for (String animal : animals) { // do something with the name } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.