LongStream filter(LongPredicate predicate)返回包含与给定谓词匹配的该流元素的流。这是一个中间操作。这些操作总是很懒惰,即执行诸如filter()之类的中间操作实际上并不执行任何过滤,而是创建一个新的流,该新流在遍历时将包含与给定谓词匹配的初始流的元素。
用法:
LongStream filter(LongPredicate predicate) Where, LongStream is a sequence of primitive long-valued elements. LongPredicate represents a predicate (boolean-valued function) of one long-valued argument and the function returns the new stream.
示例1:LongStream上的filter()方法。
// Java code for LongStream filter
// (LongPredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.of(3L, 5L, 6L, 8L, 9L);
// Using LongStream filter(LongPredicate predicate)
// to get a stream consisting of the
// elements that gives remainder 2 when
// divided by 3
stream.filter(num -> num % 3 == 2)
.forEach(System.out::println);
}
}
输出:
5 8
示例2:LongStream上的filter()方法。
// Java code for LongStream filter
// (LongPredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.of(-2L, -1L, 0L, 1L, 2L);
// Using LongStream filter(LongPredicate predicate)
// to get a stream consisting of the
// elements that are greater than 0
stream.filter(num -> num > 0)
.forEach(System.out::println);
}
}
输出:
1 2
相关用法
- Java LongStream max()用法及代码示例
- Java LongStream min()用法及代码示例
- Java LongStream average()用法及代码示例
- Java LongStream noneMatch()用法及代码示例
- Java LongStream count()用法及代码示例
- Java LongStream mapToInt()用法及代码示例
- Java LongStream peek()用法及代码示例
- Java LongStream empty()用法及代码示例
- Java LongStream allMatch()用法及代码示例
- Java LongStream anyMatch()用法及代码示例
- Java LongStream distinct()用法及代码示例
- Java LongStream reduce(long identity, LongBinaryOperator op)用法及代码示例
- Java IntStream filter()用法及代码示例
- Java DoubleStream filter()用法及代码示例
- Java Stream filter()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream filter() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。