java intfilter_Java IntStream filter()用法及代码示例
IntStream filter(IntPredicate predicate)返回一个由与给定谓词匹配的流元素组成的流。这是一个中间操作。这些操作总是很懒惰,即执行诸如filter()之类的中间操作实际上并不执行任何过滤,而是创建一个新的流,该新流在遍历时将包含与给定谓词匹配的初始流的元素。用法:IntStream filter(IntPredicate predicate)Where, Int
IntStream filter(IntPredicate predicate)返回一个由与给定谓词匹配的流元素组成的流。这是一个中间操作。这些操作总是很懒惰,即执行诸如filter()之类的中间操作实际上并不执行任何过滤,而是创建一个新的流,该新流在遍历时将包含与给定谓词匹配的初始流的元素。
用法:
IntStream filter(IntPredicate predicate)
Where, IntStream is a sequence of primitive int-valued elements.
IntPredicate represents a predicate (boolean-valued function)
of one int-valued argument and the function returns the new stream.
示例1:IntStream上的filter()方法。
// Java code for IntStream filter
// (IntPredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an IntStream
IntStream stream = IntStream.of(3, 5, 6, 8, 9);
// Using IntStream filter(IntPredicate 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:IntStream上的filter()方法。
// Java code for IntStream filter
// (IntPredicate predicate) to get a stream
// consisting of the elements of this
// stream that match the given predicate.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an IntStream
IntStream stream = IntStream.of(-2, -1, 0, 1, 2);
// Using IntStream filter(IntPredicate 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
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)