LongStream distinct() in Java with examples
Last Updated :
06 Dec, 2018
LongStream distinct() is a method in java.util.stream.LongStream. This method returns a stream consisting of the distinct elements. This is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.
Syntax :
LongStream distinct()
Where, LongStream is a sequence of
primitive long-valued elements.
Below given are some examples to understand the function in a better way.
Example 1 : Printing distinct elements of Long stream.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.of(2L, 3L,
3L, 5L, 6L, 6L, 8L);
stream.distinct().forEach(System.out::println);
}
}
|
Example 2 : Counting value of distinct elements in a stream.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.of(2L, 3L, 3L,
5L, 6L, 6L, 8L);
long total = stream.distinct().count();
System.out.println(total);
}
}
|
Share your thoughts in the comments
Please Login to comment...