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 will illustrate the most common and useful ways to work with Lists using the Guava library.

Let’s start simple – and take a look at just creating a new ArrayList using Guava syntax – without new:

List<String> names = Lists.newArrayList("John", "Adam", "Jane");

2. Reverse a List

First, let’s reverse a List using Lists.reverse() as in the following example:

@Test
public void whenReverseList_thenReversed() {
    List<String> names = Lists.newArrayList("John", "Adam", "Jane");

    List<String> reversed = Lists.reverse(names);
    assertThat(reversed, contains("Jane", "Adam", "John"));
}

3. Generate Character List from a String

Now – let’s see how to break a String apart into a list of Characters.

In the following example – we use the Lists.CharactersOf() API to create a Character List from a String “John”:

@Test
public void whenCreateCharacterListFromString_thenCreated() {
    List<Character> chars = Lists.charactersOf("John");

    assertEquals(4, chars.size());
    assertThat(chars, contains('J', 'o', 'h', 'n'));
}

4. Partition a List

Next – Let’s see how to partition a List.

In the following example – we use Lists.partition() to get consecutive sublists each of size two:

@Test
public void whenPartitionList_thenPartitioned(){
    List<String> names = Lists.newArrayList("John","Jane","Adam","Tom","Viki","Tyler");

    List<List<String>> result = Lists.partition(names, 2);

    assertEquals(3, result.size());
    assertThat(result.get(0), contains("John", "Jane"));
    assertThat(result.get(1), contains("Adam", "Tom"));
    assertThat(result.get(2), contains("Viki", "Tyler"));
}

5. Remove Duplicates From List

Now – let’s use a simple trick to remove duplicates from a List.

In the following example – we copy the elements into a Set and then we create a List back out of the remaining elements:

@Test
public void whenRemoveDuplicatesFromList_thenRemoved() {
    List<Character> chars = Lists.newArrayList('h','e','l','l','o');
    assertEquals(5, chars.size());

    List<Character> result = ImmutableSet.copyOf(chars).asList();
    assertThat(result, contains('h', 'e', 'l', 'o'));
}

6. Remove Null Values from List

Next – let’s see how to remove null values from a List.

In the following example – we remove all null values using the highly useful Iterables.removeIf() API and a predicate provided by the library itself:

@Test
public void whenRemoveNullFromList_thenRemoved() {
    List<String> names = Lists.newArrayList("John", null, "Adam", null, "Jane");
    Iterables.removeIf(names, Predicates.isNull());

    assertEquals(3, names.size());
    assertThat(names, contains("John", "Adam", "Jane"));
}

7. Convert a List to an ImmutableList

Finally – let’s see how to create an immutable copy of a List – an ImmutableList – using the ImmutableList.copyOf() API:

@Test
public void whenCreateImmutableList_thenCreated() {
    List<String> names = Lists.newArrayList("John", "Adam", "Jane");

    names.add("Tom");
    assertEquals(4, names.size());

    ImmutableList<String> immutable = ImmutableList.copyOf(names);
    assertThat(immutable, contains("John", "Adam", "Jane", "Tom"));
}

8. Conclusion

And here we are – a quick tutorial going over most of the useful things you can do with Lists using Guava.

To dig into Lists even further, check out the Predicates and Functions guide to lists as well as the in-depth guide to Joining and Splitting lists in Guava.

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