Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

Iterating over elements is one of the most fundamental operations we can execute on a collection.

In this tutorial, we'll take a look at how to iterate over elements of a Set and how it differs from the similar tasks on a List or array.

2. Getting Access to Elements in a Set

A Set, unlike a List and many other collections, isn't sequential. Their elements aren't indexed, and depending on the implementation, they may not maintain order.

That means that we can't ask about a specific element of the set by its number. Because of that, we can't use a typical for loop or any other index-based method.

2.1. Iterator

The most basic and close-to-metal method of iterating over the set is invoking the iterator method exposed by every Set:

Set<String> names = Sets.newHashSet("Tom", "Jane", "Karen");
Iterator<String> namesIterator = names.iterator();

Then we can use the obtained iterator to get elements of that Set, one by one. The most iconic way is checking if the iterator has a next element in the while loop:

while(namesIterator.hasNext()) {

We can also use the forEachRemaining method added in Java 8:


We can also mix these solutions:

String firstName = namesIterator.next(); // save first name to variable
namesIterator.forEachRemaining(System.out::println); // print rest of the names

All other methods will use an Iterator in some way under the hood.

3. Streams

Every Set exposes the spliterator() method. Because of that, a Set can be easily transformed into a Stream:


We can also leverage the rich Streams API to create more complex pipelines. For example, let's map, log, and then reduce elements of the set to a single string:

String namesJoined = names.stream()

4. Enhanced Loop

While we can't use a simple, indexed for loop to iterate over a Set, we can use the enhanced loop feature introduced in Java 5:

for (String name : names) {

5. Iterating with Index

5.1. Converting to Array

Sets aren't indexed, but we can add an index artificially. One possible solution is to simply convert the Set to some more approachable data structure like an array:

Object[] namesArray = names.toArray();
for (int i = 0; i < namesArray.length; i++) {
    System.out.println(i + ": " + namesArray[i]);

Mind that conversion to an array alone will iterate over the Set once. So, in terms of complexity, we'll be iterating over the Set twice. That may be a problem if performance is crucial.

5.2. Zipping with Index

Another approach is to create an index and zip it with our Set. While we could do it in vanilla Java, there are libraries that provide tools just for that.

For example, we can use Vavr's streams:

  .forEach(t -> System.out.println(t._2() + ": " + t._1()));

6. Summary

In this tutorial, we looked at various ways of iterating over elements of the Set instance. We explored the usage of iterators, streams, and loops, and the differences between them.

As always, examples are available over GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!