• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

7 Methods - Which is the Best way to Iterate over a Set/HashSet without an Iterator?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set<String> movies = new HashSet<>();
movies.add("Avatar");
movies.add("The Lord of the Rings");
movies.add("Titanic");

Best way to Iterate over a Set/HashSet without an Iterator?


Method 1

// Obsolete Collection
Enumeration e = new Vector(movies).elements();
while (e.hasMoreElements()) {
 System.out.println(e.nextElement());
}

Method 2

for (String movie : movies) {
 System.out.println(movie);
}

Method 3

String[] movieArray = movies.toArray(new String[movies.size()]);
for (int i = 0; i < movieArray.length; i++) {
 System.out.println(movieArray[i]);
}

Method 4

// Supported in Java 8 and above
movies.stream().forEach((movie) -> {
 System.out.println(movie);
});

Method 5

// Supported in Java 8 and above
movies.stream().forEach(movie -> System.out.println(movie));

Method 6

// Supported in Java 8 and above
movies.stream().forEach(System.out::println);

Method 7 without Streams

set.forEach(System.out::println);
 
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you check how many of those techniques use an Iterator or a Spliterator behind the scenes. I think you will find the only one is that you marked as obsolete, and Enumeration is a forerunner of Iterator, so I think you will have to regard them all as using an iterator with a small i. Does No 4 differ from No 5? You seem to have missed:-
 
Saloon Keeper
Posts: 11471
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of those methods use an iterator, even the Vector approach (take a look at the Vector(Collection) constructor documentation).

Even if a collection allows you to access its elements without using an iterator, there is no good way to iterate over all of them without using an iterator. Just stick with iterators.

If your question actually was "Which is the best way to iterate over a set without calling the iterator() method directly?" then I would use either of the following approaches, depending on whether you need ordered traversal, transformation, concurrency or side-effects.

Simple ordered traversal:

Ordered traversal with transformation 1:

Ordered traversal with transformation 2:

Concurrency:

Side effects:


Which of the two ordered traversals with transformation to prefer is just a matter of taste.
 
Marshal
Posts: 6826
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Joseph, please UseCodeTags (that's a link) from now on.  They make your code much easier to read.  
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!