How to merge streams in Java?

Introduction

In this blog we will learn how to merge streams in Java

We want to merge stream to consolidate data from multiple streams in a single stream

Combining 2 streams into single stream

We can combine 2 streams using Stream.concat(stream1,stream2) which takes streams to be combined as arguments.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.examples;

import java.util.stream.Stream;

public class App {

    public static void main(String[] args) {
        Stream<Integer> nums1 =Stream.of(10,20,30);
        Stream<Integer> nums2 = Stream.of(11,12,13);

        Stream<Integer> combined = Stream.concat(nums1,nums2);
        combined.forEach(System.out::println);  // 10 20 30 11 12 13

    }
}

Merging stream using flatmap

In the below example we have list of List which we need to combine in single list.

The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.examples;

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

public class App {

    public static void main(String[] args) {
        List<Integer> nums1 = Arrays.asList(10,20,30,40,50);
        List<Integer> nums2 = Arrays.asList(11,12,13,14,15);

        List<List<Integer>> combined = Arrays.asList(nums1,nums2);

        List<Integer> result = combined.stream()
                .flatMap(x -> x.stream())
                .collect(Collectors.toList());

        System.out.println(result); //[10, 20, 30, 40, 50, 11, 12, 13, 14, 15]


    }
}

Use case for merging streams to get uniques hobbies of all employees.

We have list of employees.

Each employee has a list of hobbies.

Hobbies could be same for some employees.

We need to get unique hobbies all employees have.

For each employee we get list of hobbies, flatten it out using flatmap and collect it in Set.

Output of this result is a list of unique hobbies.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.examples;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) {

        List<Employee> employees = Arrays.asList(
                new Employee(1,"Harish",Arrays.asList("Cricket","Music","Books")),
                new Employee(2,"Tim",Arrays.asList("Football","Dancing","Books")),
                new Employee(3,"Mary",Arrays.asList("Chess","Music","Gardening"))
        );

        Set<String> hobbies = employees.stream()
                .map(emp -> emp.getHobbies()) // this is list of hobbies
                .flatMap(hobby -> hobby.stream())// hobby stream
                .collect(Collectors.toSet());

        System.out.println(hobbies); //[Chess, Cricket, Music, Gardening, Dancing, Books, Football]


    }
}

class Employee {

    private int empId;

    private String empName;

    private List<String> hobbies;

    public Employee(int empId, String empName, List<String> hobbies) {
        this.empId = empId;
        this.empName = empName;
        this.hobbies = hobbies;
    }

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", hobbies=" + hobbies +
                '}';
    }
}

Conclusion

I hope this clarifies how to merge streams in Java using Stream API.