Java Streams - Stream distinct() example








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  ava  2  s  .c  om*/
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.