Thursday, January 6, 2022

Java Stream - concat() With Examples

concat() method in Java Stream API takes two streams as arguments and returns a concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

Syntax of concat() method

concat(Stream<? extends T> a, Stream<? extends T> b)

Here parameters are-

  • a - the first stream
  • b - the second stream

Method returns a merged stream

The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel.

concat() method Java examples

1. Using concat() method to merge two streams of integers.

import java.util.stream.Stream;

public class StreamConcat {

  public static void main(String[] args) {
    Stream<Integer> stream1 = Stream.of(1, 2, 3);
      Stream<Integer> stream2 = Stream.of(4, 5, 6);
      Stream<Integer> mergedStream = Stream.concat(stream1, stream2);
      mergedStream.forEach(System.out::println);
  }
}

Output

1
2
3
4
5
6

2. Merging two Collections (Lists) by using concat() method of Java Stream API.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamConcat {

  public static void main(String[] args) {
    List<String> strList1 = Arrays.asList("A","B","C","D");
    List<String> strList2 = Arrays.asList("E","F","G","H");
    // Getting streams using Lists as source
    Stream<String> stream1 = strList1.stream();
    Stream<String> stream2 = strList2.stream();
    
      Stream<String> mergedStream = Stream.concat(stream1, stream2);
      mergedStream.forEach(System.out::println);
  }
}

Output

A
B
C
D
E
F
G
H

3. You can also use concat() method along with other methods of the Java Stream, for example you can write a program to merge 2 lists while removing duplicates which can be done by using distinct() method.

public class StreamConcat {

  public static void main(String[] args) {
    List<String> strList1 = Arrays.asList("A","B","C","D");
    List<String> strList2 = Arrays.asList("E","B","G","A");
    // Getting streams using Lists as source
    Stream<String> stream1 = strList1.stream();
    Stream<String> stream2 = strList2.stream();
    
      Stream<String> mergedStream = Stream.concat(stream1, stream2).distinct();
      mergedStream.forEach(System.out::println);
  }
}

Output

A
B
C
D
E
G

4. Using concat() to merge multiple streams. You can also merge more than two streams by nesting the concat method.

public class StreamConcat {

  public static void main(String[] args) {
      Stream<Integer> stream1 = Stream.of(1, 2);
      Stream<Integer> stream2 = Stream.of(3, 4, 5);
      Stream<Integer> stream3 = Stream.of(7, 8, 9);
      Stream<Integer> stream4 = Stream.of(10, 11);
      Stream<Integer> mergedStream = Stream.concat(stream1, 
              Stream.concat(Stream.concat(stream2, stream3), stream4));
      mergedStream.forEach(e -> System.out.print(e + " "));
  }
}

Output

1 2 3 4 5 7 8 9 10 11 

That's all for this topic Java Stream - concat() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Java Stream - count() With Examples
  2. Java Stream - boxed() With Examples
  3. Java Stream - findFirst() With Examples
  4. Java Stream - sorted() With Examples
  5. Java Stream - skip() With Examples

You may also like-

  1. Reflection in Java - Getting Class Information
  2. Java do-while Loop With Examples
  3. strictfp in Java
  4. Count Number of Times Each Character Appears in a String Java Program
  5. Stack Implementation in Java Using Linked List
  6. Class And Object in Python
  7. Spring WebFlux Example Using Functional Programming - Spring Web Reactive
  8. Highlight Currently Selected Menu Item Angular Routing Example

No comments:

Post a Comment