Java Streams - LongStream distinct() example








LongStream distinct() returns a stream consisting of the distinct elements of this stream. This is a stateful intermediate operation.

Syntax

distinct has the following syntax.

LongStream distinct()

Example

The following example shows how to use distinct.

import java.util.stream.LongStream;

public class Main {
  public static void main(String[] args) {
    LongStream b = LongStream.of(1L, 1L, Long.MAX_VALUE, Long.MIN_VALUE);

    b.distinct().forEach(System.out::println);
  }
}

The code above generates the following result.