Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

As always, the complete source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.