IntStream distinct() 是java中的一种方法。util。流动IntStream。此方法返回由不同元素组成的流。这是一个有状态的中间操作,也就是说,在处理新元素时,它可以合并以前看到的元素的状态。
null
语法:
IntStream distinct() Where, IntStream is a sequence of primitive int-valued elements.
下面给出了一些例子,以更好地理解函数。 例1: 打印整数流的不同元素。
// Java code for IntStream distinct() import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream IntStream stream = IntStream.of( 2 , 3 , 3 , 5 , 6 , 6 , 8 ); // Displaying only distinct elements // using the distinct() method stream.distinct().forEach(System.out::println); } } |
输出:
2 3 5 6 8
例2: 流中不同元素的计数值。
// Java code for IntStream distinct() method // to count the number of distinct // elements in given stream import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream IntStream stream = IntStream.of( 2 , 3 , 3 , 5 , 6 , 6 , 8 ); // storing the count of distinct elements // in a variable named total long total = stream.distinct().count(); // displaying the total number of elements System.out.println(total); } } |
输出:
5
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END