In this post, we will see how to convert list of Users to employees in java8 way. In the following examples, we can use the below approaches:

  • Using conversion function inside of map
  • Java8 function : When conversion is complex and need to do lot of fields then custom function mapper is usefull
  • Using Constructor method reference inside of map

In the below Example, We create list of users and want to transform to Employees with userId as employee Id and firstName+last name as employeeName:

Approach 1: Using conversion inside of map


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @author Javasavvy
 *
 */
public class JavaStreamExamples {

	public static void main(String[] args) {

		JavaStreamExamples eg = new JavaStreamExamples();
		List<User> users = eg.prepareData();
		List<Employee> employees = users.stream().
				                 map((user) -> new Employee(user.getUserId(),
			                   	user.getFirstName() + " " + user.getLastName(), 
			                   	user.getEmail())).
				                collect(Collectors.toList());
		employees.stream().map(e -> e.getEmployeeId() + "," + e.getEmail() + ',' + e.getEmployeeName())
				.forEach(System.out::println);

	}
	
	public List<User> prepareData() {

		User u1 = new User(101L,"[email protected]", "Jayaram","p");
		User u2 =  new User(102L,"[email protected]", "vijay","r");
		User u3 = new User(103L,"[email protected]", "Raghu","P");
		User u4 = new User(104L,"[email protected]", "Raju","r");
		User u5 = new User(105L,"[email protected]", "Ramu","j");
		User u6 =  new User(106L,"[email protected]", "Vinay","p");
		User u7 =  new User(107L,"[email protected]", "Krishna","L");
		return Arrays.asList(u1,u2,u3,u4,u5,u6,u7);

	}
}

Approach 2: Using java8 Function Mapper

In this example, userToEmployeeMapper is Function to map to User to Employee

/**
 * @author Javasavvy
 *
 */
public class FunctionIdentityMapper {
	
	static Function<User, Employee> userToEmployeeMapper = user -> new Employee(user.getUserId(),
			user.getFirstName() + " " + user.getLastName(), user.getEmail());

	public static void main(String[] args) {

		FunctionIdentityMapper eg = new FunctionIdentityMapper();
		List<User> users = eg.prepareData();
		List<Employee> employees = users.stream().
				                 map(userToEmployeeMapper).
				                collect(Collectors.toList());
		employees.stream().map(e -> e.getEmployeeId() + "," + e.getEmail() + ',' + e.getEmployeeName())
				.forEach(System.out::println);

	}
	
	public List<User> prepareData() {

		User u1 = new User(101L,"[email protected]", "Jayaram","p");
		User u2 =  new User(102L,"[email protected]", "vijay","r");
		User u3 = new User(103L,"[email protected]", "Raghu","P");
		User u4 = new User(104L,"[email protected]", "Raju","r");
		User u5 = new User(105L,"[email protected]", "Ramu","j");
		User u6 =  new User(106L,"[email protected]", "Vinay","p");
		User u7 =  new User(107L,"[email protected]", "Krishna","L");
		return Arrays.asList(u1,u2,u3,u4,u5,u6,u7);

	}
}

Approach 3: Using Constructor Method Reference:

In this we pass Constructor method reference to mapper function to convert and limitation is Employee should have constructor with User as parameter.

/**
 * @author Javasavvy
 *
 */
public class ConstructorMethodReference {
	

	public static void main(String[] args) {

		ConstructorMethodReference eg = new ConstructorMethodReference();
		List<User> users = eg.prepareData();
		List<Employee> employees = users.stream().
                map(Employee::new).
               collect(Collectors.toList());
		employees.stream().map(e -> e.getEmployeeId() + "," + e.getEmail() + ',' + e.getEmployeeName())
				.forEach(System.out::println);

	}
	
	public List<User> prepareData() {

		User u1 = new User(101L,"[email protected]", "Jayaram","p");
		User u2 =  new User(102L,"[email protected]", "vijay","r");
		User u3 = new User(103L,"[email protected]", "Raghu","P");
		User u4 = new User(104L,"[email protected]", "Raju","r");
		User u5 = new User(105L,"[email protected]", "Ramu","j");
		User u6 =  new User(106L,"[email protected]", "Vinay","p");
		User u7 =  new User(107L,"[email protected]", "Krishna","L");
		return Arrays.asList(u1,u2,u3,u4,u5,u6,u7);

	}
}

User Object


/**
 * @author Javasavvy
 */
public class User {

	private long userId;
	private String email;
	private String firstName;
	private String lastName;
	
	
	public User(long userId, String email, String firstName, String lastName) {
		super();
		this.userId = userId;
		this.email = email;
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public long getUserId() {
		return userId;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
}

Employee


/**
 * @author Javasavvy
 */
public class Employee {

	private long employeeId;
	private String employeeName;
	private String email;
	
	public Employee(long employeeId, String employeeName, String email) {
		super();
		this.employeeId = employeeId;
		this.employeeName = employeeName;
		this.email = email;
	}
	
	public Employee(User user) {
		super();
		this.employeeId = user.getUserId();
		this.employeeName = user.getFirstName()+" "+user.getLastName();
		this.email = user.getEmail();
	}
	
	public long getEmployeeId() {
		return employeeId;
	}
	public void setEmployeeId(long employeeId) {
		this.employeeId = employeeId;
	}
	public String getEmployeeName() {
		return employeeName;
	}
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
}

Thanks For Reading..