java stream过滤
Java Stream filter can be very helpful when you want to do some processing only on some elements of the Stream based on certain condition.
当您希望仅基于特定条件对Stream的某些元素进行某些处理时,Java Stream过滤器可能会非常有用。
[这里是图片001]
Stream was introduced in Java 8. Java Stream filter is an intermediate operation, i.e. it returns a new Stream.
Stream是Java 8中引入的。Java Stream过滤器是一个中间操作,即它返回一个新的Stream。
Let’s look at a simple example to count the even numbers in a list of integers. We can do it like below.
让我们看一个简单的示例来计算整数列表中的偶数。 我们可以像下面这样。
List intList = generateList(); //some method to create the list of integers
int evenCount = 0;
for(int i : intList){
if(i%2==0){
evenCount++;
//code to do some processing
}
}
System.out.println("Even Number Count: "+evenCount);
Now let’s see how we can do the same thing using java stream filter api.
现在,让我们看看如何使用java流过滤器api来做同样的事情。
Predicate predicate = new Predicate(){
@Override
public boolean test(Integer i) {
return (i%2==0);
}
};
List evenList = intList.parallelStream().filter(predicate).collect(Collectors.toList());
evenCount = evenList.size();
System.out.println("Even Number Count: "+evenCount);
Predicate is a functional interface that takes one argument and return boolean based on the test function. Above code can also be written as below using lambda expression.
谓词是一个功能接口,它接受一个参数并根据测试函数返回布尔值。 上面的代码也可以使用lambda表达式如下编写。
evenCount = intList.parallelStream().filter(i -> {
return (i % 2 == 0);
}).collect(Collectors.toList()).size();
Let’s look at another example where we want to filter a list of integers and keep only numbers greater than 90.
让我们看另一个示例,我们要过滤一个整数列表,并且仅保留大于90的数字。
List intList = new ArrayList<>();
for(int i=50; i<100; i++) intList.add(i);
Stream sequentialStream = myList.stream(); // we can create parallel stream too
Stream highNumsStream = sequentialStream.filter(p -> p > 90); //filter numbers greater than 90
System.out.print("High Nums greater than 90=");
highNumsStream.forEach(p -> System.out.print(p+" "));
//prints "High Nums greater than 90=91 92 93 94 95 96 97 98 99 "
Let’s say we have list of employees and we want to print names of all employees with age greater than 32. Below is a simple program to do this using java stream filter.
假设我们有一个雇员列表,并且想打印所有年龄大于32岁的雇员的姓名。下面是一个使用Java流过滤器执行此操作的简单程序。
package com.journaldev.examples;
import java.util.ArrayList;
import java.util.List;
public class StreamFilterExample {
public static void main(String[] args) {
List empList = new ArrayList<>();
empList.add(new Employee("Pankaj", 35));
empList.add(new Employee("David", 25));
empList.add(new Employee("Lisa", 31));
empList.add(new Employee("Dean", 40));
// print all employees above 32 years of age
empList.stream().filter(p -> (p.getAge() > 32)).forEach(p -> System.out.println(p.getName()));
}
}
class Employee {
private String name;
private int age;
public Employee() {
}
public Employee(String n, int a) {
this.name = n;
this.age = a;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Above program produces below output.
上面的程序产生下面的输出。
Pankaj
Dean
If we want to get the first employee only with age over 32, then we can use filter with findFirst method as shown below.
如果我们只想获取年龄在32岁以上的第一位员工,则可以将filter与findFirst方法结合使用,如下所示。
Optional empO = empList.stream().filter(p -> (p.getAge() > 32)).findFirst();
if(empO.isPresent())
System.out.println(empO.get().getName()+","+empO.get().getAge());
That’s all for java stream filter examples.
Java流过滤器示例的全部内容。
Reference: Java 8 Official Documentation on Stream Filter
参考: 有关流过滤器的Java 8官方文档
翻译自: https://www.journaldev.com/14068/java-stream-filter
java stream过滤