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

List is a pretty commonly used data structure in Java. Sometimes, we want to case-insensitively check if a string is an element in a string list.

In this quick tutorial, we’ll explore various methods and strategies to solve this common problem in Java.

2. Introduction to the Problem

List provides the convenient contains() method to check if a given value exists in the list. Under the hood, List.contains() compares the given object with each element in the list through the equals() method.

Therefore, if the list is a List<String>, the contains() method only compares strings case-sensitively. For example, we have a list of movie titles:

List<String> THE_LIST = List.of("Game of Thrones", "Forrest Gump", "American Beauty", "Pretty Woman", "Catch Me If You Can");

When we check whether it has “catch me if you can” using the contains() method, it returns false:

assertFalse(THE_LIST.contains("catch me if you can"));

However, in many cases, we want the contains() method to support case-ignored checks. Unfortunately, the standard contains() doesn’t offer us this option. So next, let’s see how to achieve our goal.

For simplicity, we’ll leverage unit test assertions to verify whether each approach works as expected.

3. Using a Loop

We know the String class provides the equalsIgnoreCase() method, which does case-insensitive equality checks. Therefore, the first idea to solve our problem is looping through the list and using the equalsIgnoreCase() method to check each element and the given value:

boolean ignoreCaseContainsForLoop(List<String> list, String value) {
    for (String e : list) {
        if (value.equalsIgnoreCase(e)) {
            return true;
        }
    }
    return false;
}

As the code above shows, we used a for-loop to check each element in the list. Once the equalsIgnoreCase() method reports true on an element, we return true immediately and stop checking further. Otherwise, if no match is found among all the elements in the list, the method returns false.

We can create a test to verify whether the ignoreCaseContainsForLoop() method works as expected:

assertTrue(ignoreCaseContainsForLoop(THE_LIST, "CATCH me if you CAN"));
assertTrue(ignoreCaseContainsForLoop(THE_LIST, "game of thrones"));
assertFalse(ignoreCaseContainsForLoop(THE_LIST, "The Godfather"));

4. Using the Stream API

Java introduced the Stream API starting from version 8. The Stream API provides a powerful mechanism for efficiently and effectively processing collections.

Next, let’s solve our problem using the Stream API:

assertTrue(THE_LIST.stream().anyMatch(e -> e.equalsIgnoreCase("CATCH me if you CAN")));

As demonstrated, we utilized the anyMatch() method from the Stream API to ascertain whether an element matches our criteria. We conveyed our criteria to anyMatch() using a lambda expression.

Alternatively, we have the option to employ a method reference to pass the predicate to the anyMatch() method:

assertTrue(THE_LIST.stream().anyMatch("game of thrones"::equalsIgnoreCase));
assertFalse(THE_LIST.stream().anyMatch("The Godfather"::equalsIgnoreCase));

5. Conclusion

In this article, we’ve explored two approaches to performing a case-insensitive check to determine if a string list contains a specific string.

First, we tackled the problem by crafting a traditional loop-based method. Next, we harnessed the power of the Stream API’s anyMatch() method to accomplish the same objective.

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