In this article we will see how to convert a Java 8 stream of one type to another type, we will use "Employee" class list "employees" to be converted into various other forms:
class Employee {
	private int id;
	private String name;

	// Getter Setters, constructor and toString()

}


1) Covert stream of Object to Stream of String in Java 8

		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));
		employees.add(new Employee(5, "John"));
		employees.add(new Employee(4, "Ben"));

		System.out.println("employees list");
		employees.forEach(i -> System.out.println(i));

		System.out.println("converted string stream");

		// covert Employee to String
		employees.stream().map(i -> i.getName()).forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:

employees 

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=5, name=John]
Employee [id=4, name=Ben]

converted string stream

Zenny
John
Ben
John
Ben


2) Covert List of Object to List of String in Java 8

		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));
		employees.add(new Employee(5, "John"));
		employees.add(new Employee(4, "Ben"));

		System.out.println("employees list");
		employees.forEach(i -> System.out.println(i));

		System.out.println("converted string stream");

		// covert Employee to String
		List<String> employeesNames = employees.stream().map(i -> i.getName()).collect(Collectors.toList());
		employeesNames.forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:

employees 

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=5, name=John]
Employee [id=4, name=Ben]

converted string stream

Zenny
John
Ben



3) Covert List of Object to List of another Object in Java 8

We can covert a list of one type of object to another type using Java 8 map() function, we will convert Employee list to Student list as shown below:
		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));
		employees.add(new Employee(5, "John"));
		employees.add(new Employee(4, "Ben"));

		System.out.println("employees list");
		employees.forEach(i -> System.out.println(i));

		System.out.println("converted student list");

		// covert Employee to Student
		List<Student> employeesNames = employees.stream().map(i -> new Student(i.getId(), i.getName(), 23))
				.collect(Collectors.toList());
		employeesNames.forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:

employees list

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=5, name=John]
Employee [id=4, name=Ben]

converted student list

Student [id=3, name=Zenny, age=23]
Student [id=1, name=John, age=23]
Student [id=2, name=Ben, age=23]
Student [id=5, name=John, age=23]
Student [id=4, name=Ben, age=23]

  • By Techburps.com
  • Oct 6, 2018
  • Java 8