How to use distinct in IntStream?

Here is an example to explain how to use distinct() in IntStream.

Syntax:

Modifier and Type         	Method
IntStream                  distinct()
package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;

public class IntStreamDistinct {

  public static void main(String[] args) {
    System.out.println("---------- Distinct Example 1 -----------------");
    //Returns a stream consisting of the distinct elements of this stream.
    IntStream intStream1 = IntStream.of(0, 0, 2, 3, 4, 6, 6).distinct();
    intStream1.forEach(System.out::println);
    System.out.println("---------- Distinct Example 2 -----------------");
    IntStream intStream2 = IntStream.builder().add(10).add(11).add(11).add(10).add(12).build().distinct();
    intStream2.forEach(System.out::println);
    System.out.println("---------------------------------------");
  }
}

Output:

---------- Distinct Example 1 -----------------
0
2
3
4
6
---------- Distinct Example 2 -----------------
10
11
12
---------------------------------------

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*