Case-Insensitive Search in Java ArrayLists

Examples of searching an element in an ArrayList ignoring its case.

Overview

Java ArrayList is a dynamically growing ordered collection of elements and is widely used. This tutorial demonstrates various ways of finding an ArrayList element ignoring its case.

ArrayList.contains() Method

The contains() method of the List interface is useful for checking if the List contains the specified element.

The method internally uses the equals() method to check for a match and returns true if the element is found. This is why the contains() method on a List of String elements performs the search in case sensitive way.

List<String> list = List.of("Ray", "Leo", "Bee", "Tim"); assertTrue(list.contains("Ray")); assertFalse(list.contains("ray"));
Code language: Java (java)

Case-Insensitive Search in Lists

To overcome this problem, we can use Java Streams API, create a utility method, or override the contains() method. The following sections demonstrate all the available ways with examples.

Creating a Static Utility Method

Utility methods help us create reusable abstractions, and we can create a static utility method to perform a text search in a List ignoring the case.

public static boolean searchIgnoringCase( List<String> list, String text) { for (String s : list) { if (s.equalsIgnoreCase(text)) { return true; } } return false; }
Code language: Java (java)

The method iterates over the given List and tries to match elements with the given text, ignoring the case. Our method returns true as soon as the match is found.

Now, we can use our static utility method to find a text in a List in a case-insensitive way.

boolean contains = searchIgnoringCase(list, "lEo"); System.out.println(contains); //prints: //true
Code language: Java (java)

Using Java Streams

The utility method in the previous example uses classic Java. However, the Java Streams API provides an even more compact way of performing the case-insensitive search in Java List.

Example of using Java Streams API to search a List of String ignoring the case.

boolean contains = list.stream() .anyMatch(x -> x.equalsIgnoreCase("raY")); System.out.println(contains); //prints: //true
Code language: Java (java)

The anyMatch() function applies the given Predicate on each element of the Stream until one of them returns true.

Overriding the contains() Method

As stated earlier, the contains() method internally uses the equals() to check for the match. However, we can extend an ArrayList<String> and override the contains() method to find the match ignoring case.

Override the contains() method by extending an ArrayList of String elements.

class CaseInsensitiveArrayList extends ArrayList<String> { public CaseInsensitiveArrayList(Collection<String> c) { super(c); } @Override public boolean contains(Object o) { String text = (String) o; return this.stream() .anyMatch(x -> x.equalsIgnoreCase(text)); } }
Code language: Java (java)

The overridden contains() method uses the Java Streams API for the case-insensitive search. The constructor allows us to create a new instance containing all the elements of any existing List of Strings.

Let’s use our custom implementation of ArrayList to perform the case-insensitive List search.

List<String> customList = new CaseInsensitiveArrayList(list); System.out.println(customList.contains("raY")); //prints: //true
Code language: Java (java)

Subclassing the ArrayList like this has one more advantage the overridden contains() method also modifies the behaviour of the containsAll() method. The containsAll() method internally uses contains(). Thus, when we change the contains() method to search in a case-insensitive way, the containsAll() method also works in a case-insensitive manner.
Remember that the containsAll() method returns true if the List contains every element from the given Collection.

Let’s use the List.containsAll() method to perform the case-insensitive text search on a List of Strings.

System.out.println( customList.containsAll(List.of("ray", "bEE", "tIm"))); //prints: //true
Code language: Java (java)

Summary

This tutorial taught us how to perform case-insensitive text searches in a List of Strings. By default, the contains() method uses equals(), making the text searches case-sensitive.

However, we can use classic Java to Streams API to create a static utility method to find an element in a Java List in ignoring the case. Alternatively, we can create a subclass of the ArrayList<String> and override the contains() method to match elements irrespective of the case. Overriding the contains() method also modifies the behaviour of the containsAll().

Access the complete source code at our GitHub Repository.

More like this: