1. Overview
1.概述
In this tutorial, we’ll explore the use of the Stream.count() method. Specifically, we’ll see how we can combine the count() method with the filter() method to count the matches of a Predicate we’ve applied.
在本教程中,我们将探索Stream.count()方法的使用。具体来说,我们将看到如何将count()方法与filter()方法相结合,以计算我们应用的Predicate的匹配。
2. Using Stream.count()
2.使用Stream.count()
The count() method itself provides a small but very useful functionality. We can also combine it excellently with other tools, for example with Stream.filter().
count()方法本身提供了一个小但非常有用的功能。我们还可以将它与其他工具出色地结合起来,例如与Stream.filter()。
Let’s use the same Customer class that we defined in our tutorial for Stream.filter():
让我们使用我们在我们的Stream.filter()教程中定义的同一个Customer类。
public class Customer {
private String name;
private int points;
//Constructor and standard getters
}
In addition, we also create the same collection of customers:
此外,我们还创建了相同的客户集合。
Customer john = new Customer("John P.", 15);
Customer sarah = new Customer("Sarah M.", 200);
Customer charles = new Customer("Charles B.", 150);
Customer mary = new Customer("Mary T.", 1);
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
Next, we’ll apply Stream methods on the list to filter it and determine how many matches our filters get.
接下来,我们将在列表上应用Stream方法来过滤它,并确定我们的过滤器得到多少个匹配。
2.1. Counting Elements
2.1.计算元素
Let’s see the very basic usage of count():
让我们看看count()的最基本用法。
long count = customers.stream().count();
assertThat(count).isEqualTo(4L);
Note that count() returns a long value.
注意,count()返回一个long值。
2.2. Using count() With filter()
2.2.使用count()与filter()
The example in the previous subsection wasn’t really impressive. We could have come to the same result with the List.size() method.
上一小节的例子其实并不令人印象深刻。我们可以用List.size()方法得出同样的结果。
Stream.count() really shines when we combine it with other Stream methods – most often with filter():
Stream.count()当我们把它与其他Stream方法结合起来时,才真正发挥了作用–最常见的是与filter()结合:
long countBigCustomers = customers
.stream()
.filter(c -> c.getPoints() > 100)
.count();
assertThat(countBigCustomers).isEqualTo(2L);
In this example, we’ve applied a filter on the list of customers, and we’ve also obtained the number of customers that fulfill the condition. In this case, we have two customers with more than 100 points.
在这个例子中,我们在客户名单上应用了一个过滤器,我们也得到了满足条件的客户数量。在这个例子中,我们有两个超过100分的客户。
Of course, it can also happen that no element matches our filter:
当然,也可能发生没有元素符合我们的过滤器。
long count = customers
.stream()
.filter(c -> c.getPoints() > 500)
.count();
assertThat(count).isEqualTo(0L);
2.3. Using count() With Advanced Filters
2.3.使用count()和高级过滤器
In our tutorial about filter(), we saw some more advanced use cases of the method. Of course, we can still count the result of such filter() operations.
在我们关于filter()的教程中,我们看到了该方法的一些更高级的用例。当然,我们仍然可以计算这种filter()操作的结果。
We can filter collections with multiple criteria:
我们可以用多个标准来过滤集合:。
long count = customers
.stream()
.filter(c -> c.getPoints() > 10 && c.getName().startsWith("Charles"))
.count();
assertThat(count).isEqualTo(1L);
Here, we filtered and counted the number of customers whose names start with “Charles” and who have more than 10 points.
在这里,我们过滤并统计了名字以 “查尔斯 “开头且积分超过10分的客户数量。
We can also extract the criteria into its own method and use method reference:
我们也可以将标准提取到自己的方法中并使用方法引用:。
long count = customers
.stream()
.filter(Customer::hasOverHundredPoints)
.count();
assertThat(count).isEqualTo(2L);
3. Conclusion
3.结论
In this article, we saw some examples of how to use the count() method in combination with the filter() method to process streams. For further use cases of count(), check out other methods that return a Stream, such as those shown in our tutorial about merging streams with concat().
在这篇文章中,我们看到了一些如何使用count()方法与filter()方法相结合来处理流的例子。对于count()的进一步使用案例,请查看其他返回Stream的方法,例如我们关于用concat()合并流的教程中展示的方法。
As always, the complete code is available over on GitHub.
一如既往,完整的代码可在GitHub上获得,。