Java DoubleStream filter()示例
DoubleStream filter(DoublePredicate predicate) 返回一个由这个流中符合给定谓词的元素组成的流。这是一个 中间操作。 这些操作总是懒惰的,即执行中间操作如filter()实际上并不执行任何过滤,而是创建一个新的流,当遍历时,包含初始流中符合给定谓词的元素。
语法:
DoubleStream filter(DoublePredicate predicate)
参数
- DoubleStream : 一个原始双值元素的序列。
- DoublePredicate : 一个双值参数的谓词(布尔值函数)。
返回值: 该函数返回新的流。
例1: DoubleStream的filter()方法。
// Java code for DoubleStream filter
// (DoublePredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an DoubleStream
DoubleStream stream = DoubleStream.of(3.6, 5.4, 6.6, 8.1, 9.7);
// Using DoubleStream filter(DoublePredicate predicate)
// to get a stream consisting of the
// elements that are greater than 5.7
stream.filter(num -> num > 5.7)
.forEach(System.out::println);
}
}
输出:
6.6
8.1
9.7
例2: DoubleStream的filter()方法。
// Java code for DoubleStream filter
// (DoublePredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an DoubleStream
DoubleStream stream = DoubleStream.of(3.6, 5.4, 6.6, 8.1, 9.7);
// Using DoubleStream filter(DoublePredicate
// predicate) to get a stream consisting of
// the elements that when divided by 2
// gives quotient > 2.3
stream.filter(num -> num / 2.0 > 2.3)
.forEach(System.out::println);
}
}
输出:
5.4
6.6
8.1
9.7