Stream distinct() example

Description

Stream distinct() returns a stream consisting of the distinct elements according to Object.equals(Object).

Syntax

distinct has the following syntax.


Stream<T> distinct()

Example

The following example shows how to use distinct.


import java.util.Arrays;
import java.util.List;
/*from w w w .j  a va  2  s . c  o m*/
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("1","1","2","3","4");

    stringList.stream()
           .distinct()
           .forEach(System.out::println);
  }
}

The code above generates the following result.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder