1. Overview
1.概述
In this tutorial, we’ll look into different ways to search for a String in an ArrayList. Our intent is to check if a specific non-empty sequence of characters is present in any of the elements in the ArrayList and to return a list with all the matching elements.
在本教程中,我们将研究在一个ArrayList中搜索一个String的不同方法。我们的目的是检查ArrayList中的任何元素是否存在一个特定的非空字符序列,并返回一个包含所有匹配元素的列表。
2. Basic Looping
2.基本循环
First, let’s use a basic loop to search the sequence of characters in the given search string using the contains method of Java’s String class:
首先,让我们使用一个基本的循环,使用Java的String类的contains方法搜索给定搜索字符串中的字符序列。
public List<String> findUsingLoop(String search, List<String> list) {
List<String> matches = new ArrayList<String>();
for(String str: list) {
if (str.contains(search)) {
matches.add(str);
}
}
return matches;
}
3. Streams
3.溪流
The Java 8 Streams API provides us with a more compact solution by using functional operations.
Java 8 Streams API通过使用函数式操作为我们提供了一个更紧凑的解决方案。
First, we’ll use the filter() method to search our input list for the search string, and then, we’ll use the collect method to create and populate a list containing the matching elements:
首先,我们将使用filter()方法来搜索我们的输入列表的搜索字符串,然后,我们将使用collect方法来创建和填充一个包含匹配元素的列表。
public List<String> findUsingStream(String search, List<String> list) {
List<String> matchingElements = list.stream()
.filter(str -> str.trim().contains(search))
.collect(Collectors.toList());
return matchingElements;
}
4. Third-Party Libraries
4.第三方图书馆
If we cannot use the Java 8 Stream API, we can look at third-party libraries such as Commons Collections and Google Guava.
如果我们不能使用Java 8 Stream API,我们可以看看第三方库,如Commons Collections和Google Guava.。
To use them, we just need to add Guava, Commons Collections, or both dependencies in our pom.xml file:
要使用它们,我们只需要在我们的pom.xml文件中添加Guava、Commons Collections或两者的依赖。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
4.1. Commons Collections
4.1.公用事业集合
Commons Collections provides us with a method IterableUtils.filteredIterable() that matches the given Iterable against a Predicate.
Commons Collections为我们提供了一个方法IterableUtils.filteredIterable(),它可以将给定的Iterable与Predicate进行匹配。
Let’s call IterableUtils.filteredIterable(), defining the predicate to select only those elements containing the search string. Then, we’ll use IteratorUtils.toList() to convert the Iterable to a List:
让我们调用IterableUtils.filteredIterable(),定义谓词,只选择那些包含搜索字符串的元素。然后,我们将使用IteratorUtils.toList()来将Iterable转换为List。
public List<String> findUsingCommonsCollection(String search, List<String> list) {
Iterable<String> result = IterableUtils.filteredIterable(list, new Predicate<String>() {
public boolean evaluate(String listElement) {
return listElement.contains(search);
}
});
return IteratorUtils.toList(result.iterator());
}
4.2. Google Guava
4.2. Google Guava
Google Guava offers a similar solution to Apache’s filteredIterable() with the Iterables.filter() method. Let’s use it to filter the list and return only the elements matching our search string:
Google Guava提供了一个与Apache的filteredIterable()类似的解决方案,即Iterables.filter()方法。让我们用它来过滤列表,只返回与我们的搜索字符串匹配的元素。
public List<String> findUsingGuava(String search, List<String> list) {
Iterable<String> result = Iterables.filter(list, Predicates.containsPattern(search));
return Lists.newArrayList(result.iterator());
}
5. Conclusion
5.总结
In this tutorial, we’ve learned different ways of searching for a String in an ArrayList. We first started with a simple for loop and then proceeded with an approach using the Stream API. Finally, we saw some examples using two third-party libraries — Google Guava and Commons Collections.
在本教程中,我们学习了在ArrayList中搜索String的不同方法。我们首先从一个简单的for循环开始,然后进行了使用Stream API的方法。最后,我们看到了一些使用两个第三方库的例子 – Google Guava和Commons Collections.。
The complete examples are available over on GitHub.
完整的例子可在GitHub上找到。