Collect and Convert Objects using Lambda Expression in Java 8


In this tutorial, I will show you how you are going to collect and convert objects using lambda expression in Java 8 stream API. We will collect objects and map to another custom objects using Java 8 stream API with the help of Lambda Expression.

Suppose you have entity classes generated from your database tables and after fetching data from database tables you want to apply some logic and want to map to custom Data Transfer Objects.

So here is the example which shows how to do that. This example does not use any database here.


Java 1.8, Eclipse Neon

Example and Source Code

Create below entity classes. Ideally, entity classes should be mapped to database tables. But for our example we will assume that we are working on entity classes.

public class PersonEntity {
	private int id;
	private String name;
	private String email;
	public PersonEntity() {
	public PersonEntity(int id, String name, String email) { = id; = name; = email;
	// getters and setters

The below class holds a list of PersonEntity objects.

import java.util.List;
public class PersonGroupEntity {
	private String grpName;
	private List<PersonEntity> persons;
	public PersonGroupEntity() {
	public PersonGroupEntity(String grpName, List<PersonEntity> persons) {
		this.grpName = grpName;
		this.persons = persons;
        //getters and setters

Creating DTO Class

Create DTO classes so that Entity objects will be mapped into DTO objects using Java 8 Lambda Expression.

public class PersonDto {
	private int id;
	private String firstName;
	private String lastName;
	private String emailAddress;
	//getters and setters
	public String toString() {
		return "PersonDto [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailAddress="
				+ emailAddress + "]";

Similarly the below class will hold a list of PersonDto objects.

import java.util.ArrayList;
import java.util.List;
public class PersonGroupDto {
	private String groupName;
	private List<PersonDto> persons;
	public String getGroupName() {
		return groupName;
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	public List<PersonDto> getPersons() {
		if (persons == null) {
			persons = new ArrayList<>();
		return persons;
	public void setPersons(List<PersonDto> persons) {
		this.persons = persons;
	public String toString() {
		return "PersonGroupDto [groupName=" + groupName + ", persons=" + persons + "]";

Converting using Lambda

Now create below main class that will be used to convert using lambda expression and test the example using dummy values.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CollectListOfObjectsUsingStream {
	public static void main(String[] args) {
		PersonEntity pe1 = new PersonEntity(1000, "Soumitra roy", "[email protected]");
		PersonEntity pe2 = new PersonEntity(1001, "arup chatterjee", "[email protected]");
		PersonEntity pe3 = new PersonEntity(1002, "Souvik Sanyal", "[email protected]");
		PersonEntity pe4 = new PersonEntity(1003, "suman Mukherjee", "[email protected]");
		PersonEntity pe5 = new PersonEntity(1004, "soumitra sarkar", "[email protected]");
		PersonEntity pe6 = new PersonEntity(1005, "Anjel Ghosh", "[email protected]");
		PersonEntity pe7 = new PersonEntity(1006, "suman Pandit", "[email protected]");
		PersonEntity pe8 = new PersonEntity(1007, "Ranjit pandit", "[email protected]");
		PersonEntity pe9 = new PersonEntity(1008, "TK pal", "[email protected]");
		PersonEntity pe10 = new PersonEntity(1009, "Abhisekh paul", "[email protected]");
		PersonEntity pe11 = new PersonEntity(1010, "Sumanjit raut", "[email protected]");
		PersonGroupEntity pg1 = new PersonGroupEntity("Product", new ArrayList<PersonEntity>(Arrays.asList(pe9)));
		PersonGroupEntity pg2 = new PersonGroupEntity("Service",
				new ArrayList<PersonEntity>(Arrays.asList(pe1, pe2, pe3, pe4, pe5, pe7, pe10, pe11)));
		PersonGroupEntity pg3 = new PersonGroupEntity("Solution", new ArrayList<PersonEntity>(Arrays.asList(pe6)));
		PersonGroupEntity pg4 = new PersonGroupEntity("Teaching", new ArrayList<PersonEntity>(Arrays.asList(pe8)));
		PersonGroupEntity pg5 = new PersonGroupEntity("Core", null);
		List<PersonGroupEntity> personGroupEntities = new ArrayList<>(Arrays.asList(pg1, pg2, pg3, pg4, pg5));
		List<PersonGroupDto> personGroupDtos = -> {
			PersonGroupDto personGroupDto = new PersonGroupDto();
			if (pg.getPersons() != null) {
				List<PersonDto> personDtos = pg.getPersons().stream().map(p -> {
					PersonDto personDto = new PersonDto();
					String[] fullName = splitNamesInitCharCapitalize(p.getName());
					return personDto;
			} else {
				personGroupDto.setPersons(new ArrayList<>());
			return personGroupDto;
		personGroupDtos.forEach(pg -> System.out.println(pg));
	private static String[] splitNamesInitCharCapitalize(String name) {
		String[] splits = name.split("\\s");
		String firstNameInitChar = splits[0].substring(0, 1).toUpperCase();
		String lastNameInitChar = splits[1].substring(0, 1).toUpperCase();
		String[] fullName = new String[] { firstNameInitChar + splits[0].substring(1),
				lastNameInitChar + splits[1].substring(1) };
		return fullName;


PersonGroupDto [groupName=Product, persons=[PersonDto [id=1008, firstName=TK, lastName=Pal, [email protected]]]]
PersonGroupDto [groupName=Service, persons=[PersonDto [id=1000, firstName=Soumitra, lastName=Roy, [email protected]], PersonDto [id=1001, firstName=Arup, lastName=Chatterjee, [email protected]], PersonDto [id=1002, firstName=Souvik, lastName=Sanyal, [email protected]], PersonDto [id=1003, firstName=Suman, lastName=Mukherjee, [email protected]], PersonDto [id=1004, firstName=Soumitra, lastName=Sarkar, [email protected]], PersonDto [id=1006, firstName=Suman, lastName=Pandit, [email protected]], PersonDto [id=1009, firstName=Abhisekh, lastName=Paul, [email protected]], PersonDto [id=1010, firstName=Sumanjit, lastName=Raut, [email protected]]]]
PersonGroupDto [groupName=Solution, persons=[PersonDto [id=1005, firstName=Anjel, lastName=Ghosh, [email protected]]]]
PersonGroupDto [groupName=Teaching, persons=[PersonDto [id=1007, firstName=Ranjit, lastName=Pandit, [email protected]]]]
PersonGroupDto [groupName=Core, persons=[]]

Source Code

You can download source code.

Thanks for reading.

Related posts

Leave a Comment