How to use flatMap() in Java 8 Streams example

In this article we will see how to convert list of list to plain list in Java 8 Streams using flatMap(), we will use "Employee" class's list of list "listOfList" to be converted into a single list "mergedList".
class Employee {
	private int id;
	private String name;

	// Getter Setters, constructor and toString()


How to use flatMap() in Java 8 streams

public static void main(String[] args) {
		List<Employee> employees1 = new ArrayList<>();
		employees1.add(new Employee(3, "Zenny"));
		employees1.add(new Employee(1, "John"));

		List<Employee> employees2 = new ArrayList<>();
		employees2.add(new Employee(3, "Zenny"));
		employees2.add(new Employee(1, "John"));

		List<List<Employee>> listOfList = new ArrayList<>();

		System.out.println("List of list");
		listOfList.forEach(i -> System.out.println(i));

		List<Employee> mergedList =;
		System.out.println("Plain list ");
		mergedList.forEach(i -> System.out.println(i));

Output: Output of above code will look something like this:

List of list

[Employee [id=3, name=Zenny], Employee [id=1, name=John]]
[Employee [id=3, name=Zenny], Employee [id=1, name=John]]

Plain list 

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=3, name=Zenny]
Employee [id=1, name=John]