s-day07- Stream流中的常用方法_filter
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.stream07;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Stream流中的常用方法_filter
|
||||||
|
Stream<T> filter(Predicate<? super T> predicate) 对指定的流进行过滤操作
|
||||||
|
知识点回顾:
|
||||||
|
Predicate:接收一个参数,返回一个boolean的结果,用来进行数据筛选
|
||||||
|
抽象方法:boolean test(T t) ---> t->{return ...}
|
||||||
|
|
||||||
|
filter的执行原理:它会将stream流中的每个元素,作为以上Lambda表达式的参数,执行方法体,如果返回的是true,那就会把当前元素保存在新流中,如果是false,舍弃数据
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class FilterDemo15 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||||
|
/*//只要获取大于3的整数,其他不要
|
||||||
|
Stream<Integer> newStream = stream.filter(i -> i > 3);
|
||||||
|
//看一下流中的数据
|
||||||
|
newStream.forEach(i-> System.out.println(i));*/
|
||||||
|
stream.filter(i->i>3).forEach(i-> System.out.println(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user