Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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()) {
   System.out.println(namesIterator.next());
}

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

namesIterator.forEachRemaining(System.out::println);

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:

names.stream().forEach(System.out::println);

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()
    .map(String::toUpperCase)
    .peek(System.out::println)
    .collect(Collectors.joining());

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) {
    System.out.println(name);
}

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:

Stream.ofAll(names)
  .zipWithIndex()
  .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.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW