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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll explore different ways to convert an Iterable to a Collection in Java.

We’ll start with plain Java solutions, then have a look at the options that the Guava and Apache Commons libraries also provide.

2. Iterable and Iterator

First, we’ll define our Iterable:

Iterable<String> iterable = Arrays.asList("john", "tom", "jane");

We’ll also define a simple Iterator – to highlight the difference between converting Iterable to Collection and Iterator to Collection:

Iterator<String> iterator = iterable.iterator();

3. Using Plain Java

3.1. Iterable to Collection

We can use the Java 8 forEach() method to add all elements to the List:

@Test
public void whenConvertIterableToListUsingJava8_thenSuccess() {
    List<String> result = new ArrayList<String>();
    iterable.forEach(result::add);

    assertThat(result, contains("john", "tom", "jane"));
}

Or use the Spliterator class to convert our Iterable to Stream then to Collection:

List<String> result = 
  StreamSupport.stream(iterable.spliterator(), false)
    .collect(Collectors.toList());

3.2. Iterator to Collection

On the other hand, instead of using forEach(), we’ll use forEachRemaining() with Iterator:

@Test
public void whenConvertIteratorToListUsingJava8_thenSuccess() {
    List<String> result = new ArrayList<String>();
    iterator.forEachRemaining(result::add);

    assertThat(result, contains("john", "tom", "jane"));
}

We can also create a Spliterator from our Iterator then use it to convert Iterator to Stream:

List<String> result = 
  StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
    .collect(Collectors.toList());

3.3. Using a For-Loop

Let’s also have a look at a solution that uses a very simple for-loop to convert our Iterable to a List:

@Test
public void whenConvertIterableToListUsingJava_thenSuccess() {
    List<String> result = new ArrayList<String>();
    for (String str : iterable) {
        result.add(str);
    }

    assertThat(result, contains("john", "tom", "jane"));
}

On the other hand, we’ll use hasNext() and next() with the Iterator:

@Test
public void whenConvertIteratorToListUsingJava_thenSuccess() {
    List<String> result = new ArrayList<String>();
    while (iterator.hasNext()) {
        result.add(iterator.next());
    }

    assertThat(result, contains("john", "tom", "jane"));
}

4. Using Guava

There are also a few libraries available that provide convenient methods to help us achieve this.

Let’s see how we can use Guava to convert from an Iterable to a List:

We can create a new List from Iterable or Iterator using Lists.newArrayList():

List<String> result = Lists.newArrayList(iterable);

Or we can use ImmutableList.copyOf():

List<String> result = ImmutableList.copyOf(iterable);

5. Using Apache Commons

Finally, we’ll use Apache Commons IterableUtils to create a List from Iterable:

List<String> result = IterableUtils.toList(iterable);

Similarly, we’ll use IteratorUtils to create a List from our Iterator:

List<String> result = IteratorUtils.toList(iterator);

6. Conclusion

In this short article, we learned how to convert an Iterable and Iterator to a Collection using Java. We explored different ways using plain Java, and two external libraries: Guava and Apache Commons.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW