1. Overview
1.概述
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.
List 是 Java 中非常常用的数据结构。有时,我们想不区分大小写地检查字符串是否是字符串列表中的元素。
In this quick tutorial, we’ll explore various methods and strategies to solve this common problem in Java.
在本快速教程中,我们将探讨 Java 中解决这一常见问题的各种方法和策略。
2. Introduction to the Problem
2.问题介绍
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.
List 提供了方便的 contains() 方法来检查给定值是否存在于列表中。在引擎盖下,List.contains() 通过 equals() 方法将给定对象与列表中的每个元素进行比较。
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>,contains()方法只能比较字符串的大小写。例如,我们有一个电影标题列表:
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:
当我们使用contains()方法检查它是否具有”catch me if you can“时,它返回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.
但是,在许多情况下,我们希望 contains() 方法支持忽略大小写的检查。遗憾的是,标准的 contains() 并不提供这种选项。接下来,让我们看看如何实现我们的目标。
For simplicity, we’ll leverage unit test assertions to verify whether each approach works as expected.
为简单起见,我们将利用单元测试断言来验证每种方法是否按预期运行。
3. Using a Loop
3.使用循环
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:
我们知道 String 类提供了 equalsIgnoreCase() 方法,该方法可进行大小写不敏感的相等检查。因此,解决我们问题的第一个想法是 在列表中查找,并使用 equalsIgnoreCase() 方法检查每个元素和给定值:
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.
如上面的代码所示,我们使用 for 循环来检查列表中的每个元素。一旦 equalsIgnoreCase() 方法对某个元素报告 true,我们将立即返回 true,并停止进一步检查。否则,如果在列表中的所有元素中没有找到匹配元素,该方法将返回 false. 。
We can create a test to verify whether the ignoreCaseContainsForLoop() method works as expected:
我们可以创建一个测试来验证 ignoreCaseContainsForLoop() 方法是否按预期运行:
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
4.使用流应用程序接口
Java introduced the Stream API starting from version 8. The Stream API provides a powerful mechanism for efficiently and effectively processing collections.
Java 从第 8 版开始引入了 Stream API。流 API 为高效处理集合提供了强大的机制。
Next, let’s solve our problem using the Stream API:
接下来,让我们使用流 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.
如图所示,我们使用了流 API 中的 anyMatch() 方法来确定元素是否符合我们的标准。我们使用 lambda 表达式向 anyMatch() 传达了我们的标准。
Alternatively, we have the option to employ a method reference to pass the predicate to the anyMatch() method:
或者,我们可以选择使用方法引用将谓词传递给 anyMatch() 方法:
assertTrue(THE_LIST.stream().anyMatch("game of thrones"::equalsIgnoreCase));
assertFalse(THE_LIST.stream().anyMatch("The Godfather"::equalsIgnoreCase));
5. Conclusion
5.结论
In this article, we’ve explored two approaches to performing a case-insensitive check to determine if a string list contains a specific string.
在本文中,我们探讨了两种执行大小写不敏感检查以确定字符串列表是否包含特定字符串的方法。</span
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.
首先,我们采用传统的基于循环的方法来解决这一问题。接下来,我们利用流 API 的 anyMatch() 方法的强大功能来实现相同的目标。
As always, the complete source code for the examples is available over on GitHub.
一如既往,示例的完整源代码可在 GitHub 上获取。 .