25
Jul

Concatenate java Streams

import java.util.stream.Stream;

public class ConcatStreamsDemo {
	
	public static void main(String[] args) {
		// Concatenate streams using "concat()"
		Stream<String> s1 = Stream.of("NameF1", "NameL1", "Address11");
		Stream<String> s2 = Stream.of("NameF2", "NameL2", "Address22");
		Stream<String> s = Stream.concat(s1, s2);
		s.forEach(e -> System.out.print(e + ", "));

		// Remove duplicates using distinct()
		s1 = Stream.of("NameF", "NameL", "Address");
		s2 = Stream.of("NameF", "NameL", "Address");
		System.out.println("\nRemove duplicates using distinct()");
		s = Stream.concat(s1, s2).distinct();
		s.forEach(e -> System.out.print(e + ", "));
	}
}
output:
NameF1, NameL1, Address11, NameF2, NameL2, Address22, 
Remove duplicates using distinct()
NameF, NameL, Address,