Distinct and Concat Methods

Java-Distinct and Concat Methods

Distinct and Concat Methods

The distinct( ) method returns a stream that yields elements from the original stream, in the same order, except that duplicates are suppressed. The stream must obviously remember the elements that it has already seen.

Distinct and Concat Methods

You can concatenate two streams with the static concat( ) method of the Stream class.


Distinct and Concat Methods
Program

Distinct and Concat Methods

Program Source

import java.util.stream.Stream;

public class Javaapp {

    public static void main(String[] args) {
        
        Integer inary1[] = {10,20,30,40,50};
        Integer inary2[] = {60,70,80,90,100};
        Stream<Integer> istm1 = Stream.of(inary1);
        Stream<Integer> istm2 = Stream.of(inary2);
        Stream<Integer> istm3 = Stream.concat(istm1, istm2);
        System.out.print("istm1 + istm2 = istm3--> ");
        istm3.forEach((e)->System.out.print(e+","));
        System.out.println();
  
        Integer inary3[] = {10,20,50,20,40,30,80,40,50};
        Stream<Integer> istm4 = Stream.of(inary3);
        Stream<Integer> istm5 = istm4.distinct();
        System.out.print("istm5--> ");
        istm5.forEach((e)->System.out.print(e+",")); 
    }
}

Leave a Comment