1. Overview
Searching for elements in a list is a standard operation when working with ArrayList. The contains() method lets us know if a list object has the element we’re looking for.
In this tutorial, we’ll explore how to search a string in an ArrayList
2. Introduction to the Problem
Internally, the ArrayList.contains() method uses the equals() method to determine whether the list has a given element. If all elements in an ArrayList are strings, i.e., *when working with ArrayList
Let’s say we have a List object containing six strings:
List<String> LANGUAGES = Arrays.asList("Java", "Python", "Kotlin", "Ruby", "Javascript", "Go");
When we check if LANGUAGES contains “jAvA”, the contains() method reports false, as “jAvA” doesn’t equal “Java”:
String searchStr = "jAvA";
boolean result = LANGUAGES.contains(searchStr);
assertFalse(result);
In this tutorial, let’s learn a few approaches for searching a string in an ArrayList
For simplicity, we’ll use unit test assertions to verify if the solutions work as expected.
So next, let’s see them in action.
3. Using the Stream API
Java Stream API provides many handy interfaces, allowing us to handle Collections as streams easily. It’s available on Java 8 and later versions.
For example, *we can use Stream‘s anyMatch() method to do a case-insensitive string search:*
String searchStr = "koTliN";
boolean result = LANGUAGES.stream().anyMatch(searchStr::equalsIgnoreCase);
assertTrue(result);
As we can see in the example above, we search the string “koTliN” in the LANGUAGES list. Then, if we run it, the test passes.
It’s worth mentioning that the searchStr::equalsIgnoreCase we pass to the anyMatch() method is a method reference. The searchStr.equalsIgnoreCase() method will be called for each string element in the stream.
4. Creating a Utility Method
We’ve seen that the Stream API can solve the problem straightforwardly. However, we cannot use the Stream API if our Java version is older than 8. In this case, a classic approach to solving the problem is to create a utility method:
public class IgnoreCaseSearchUtil {
public static boolean ignoreCaseContains(List<String> theList, String searchStr) {
for (String s : theList) {
if (searchStr.equalsIgnoreCase(s)) {
return true;
}
}
return false;
}
}
As the code above shows, we check each string element in the given list in a for-loop. Once an element equals searchStr case-insensitively, the method returns true immediately without checking further elements in the list.
Next, let’s create a test to verify if it works as expected:
String searchStr = "ruBY";
boolean result = IgnoreCaseSearchUtil.ignoreCaseContains(LANGUAGES, searchStr);
assertTrue(result);
This time, we searched the string “ruBY” in the list. Again, the test passes if we give it a run.
5. Creating a Subclass of ArrayList
So far, we’ve learned two approaches to determining whether an ArrayList
If this is the case, we probably want to create a particular ArrayList
So next, let’s create a subclass of ArrayList
public class IgnoreCaseStringList extends ArrayList<String> {
public IgnoreCaseStringList() {
}
public IgnoreCaseStringList(Collection<? extends String> c) {
super(c);
}
@Override
public boolean contains(Object o) {
String searchStr = (String) o;
for (String s : this) {
if (searchStr.equalsIgnoreCase(s)) {
return true;
}
}
return false;
}
}
As we can see in the code above, the IgnoreCaseStringList class inherits ArrayList
Next, let’s test whether IgnoreCaseStringList works:
String searchStr = "pYtHoN";
List<String> ignoreCaseList = new IgnoreCaseStringList(LANGUAGES);
boolean result = ignoreCaseList.contains(searchStr);
assertTrue(result);
As we can see, after initializing an IgnoreCaseList instance, we can simply call the contains() method to search the given string case-insensitively. When we execute the test above, it passes. So, IgnoreCaseStringList does the job neatly.
It’s worth mentioning that the IgnoreCaseList approach brings another benefit. It makes the containsAll() method case-sensitive too. This is because the containsAll() method is implemented in the AbstractCollection class, a supertype of ArrayList. Further, it internally calls the contains() method:
public boolean containsAll(Collection<?> c) {
Iterator var2 = c.iterator();
Object e;
do {
if (!var2.hasNext()) {
return true;
}
e = var2.next();
} while(this.contains(e));
return false;
}
Finally, let’s write a test to verify it:
boolean resultContainAll = ignoreCaseList.containsAll(Arrays.asList("pYtHon", "jAvA", "koTliN", "ruBY"));
assertTrue(resultContainAll);
On the other hand, if we want the Stream API and the utility method approaches also to support the case-sensitive containsAll() feature, we must implement it on our own, for example, by adding another utility method.
6. Conclusion
In this article, we’ve explored how to perform case-insensitive searching in ArrayList
As usual, all code snippets presented in the article are available over on GitHub.