Idan Fridman

About Idan Fridman

Idan is Software engineer with experience in Server side technologies. Idan is responsible for various infrastructure models in the software industry(Telecommunications, Finance).

Mapping your Entities to DTO’s Using Java 8 Lambda expressions

We all facing the cluttered overhead code when we need to convert our DTO’S to Entities(Hibernate Entities, etc..) and backwards.

In my example ill demonstrate how the code is getting much shorter with Java 8.

Let’s create the Target DTO:

public class ActiveUserListDTO {

    public ActiveUserListDTO() {

    public ActiveUserListDTO(UserEntity userEntity) {

        this.username = userEntity.getUsername();


A simple find method to retrieve all entities using Spring data JPA API:



Find.All() method signature (like many others) returns java.lang.Iterable<T>

      java.lang.Iterable<T> findAll(java.lang.Iterable<ID> iterable)

We can’t make a Stream out of java.lang.Iterable(* Streams working on collections. Every Collection is Iterable but not every Iterable is necessary a collection).

So how do we get a Stream object in order to get Java8 Lambda’s Power?

Let’s use StreamSupport object to convert Iterable into Stream:

Stream<UserEntity> userEntityStream =, false);

Great. Now we’ve got Stream in our hands which is the key to our Java 8 labmda’s!

What’s left is to map and collect:

List<ActiveUserList> activeUserListDTOs =

I am using Java 8 Method Reference and therefor initiating (and mapping) each entity into dto.

So let’s make one short line out of everything:

List<ActiveUserList>, false).map(ActiveUserList::new).collect(Collectors.toList());

That’s neat!!


Related Articles:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

5 Responses to "Mapping your Entities to DTO’s Using Java 8 Lambda expressions"

  1. Yannick Majoros says:

    A DTO shouldn’t know about the entities it maps to. “Conversion” should be made elsewhere to avoid coupling them.

    BTW, I hope you think twice because having all entities duplicated as “DTO’s”. Is it needed in your use case? I saw too much projects where this was done blindly without added value.

  2. Idan says:

    1. Conversion” should be made elsewhere
    Please past an example I will be more than happy to adjust this.

    2. “hope you think twice because having all entities duplicated”

    In my case the DTO are being mapped back to DTO’S mostly because I am doing some data manipulation later on which I didnt want it to reflected on my Database. I find it hard to use entities as my dto’s in my project. you right it’s sometime costs us with duplicated classes but you cant avoid it.

  3. Yannick Majoros says:

    1. About conversion, I was referring to your example: ActiveUserListDTO takes a UserEntity in its constructor. Why should your dto be coupled to your entity? If it is, there is some chance that they are the same.

    2. What do you mean by “using entities as dto’s”? “You cant avoid it”, really? It’s quite common to use jpa entities in other layers nowadays. My point was that it’s more of an issue to duplicate every entity as a “dto” (please give an exact definition). If they have the same properties, they are probably the same thing anyway. Are you maybe talking about methods that you want in your entities and not in your dto’s (or vice versa)? Well, in that case, the behavior they represent shouldn’t be in the same class to begin with.

    I know someone will probably use the word “anemic”…

  4. Klaudiusz Wojtkowiak says:

    In that case DTO object imports Entity class, and has a parse logic inside, which is forbidden.
    Do You want to provide Entity classes as well? If not, there will be an import missing erros on the client side.

    DTO (Data Transfer Object) contain default contructor (no contructor in the code) and getter/setters only.
    DTO may import classes, but if’s standard JRE Java classes or another DTO as well.

  5. chuck1300 says:

    It should actually be a mapper.
    Rename for example ActiveUserListDTO to UserDtoMapper, it will have a UserDto and a method GetUserDto.
    Then change the stream :
    List userDtos =
    .map(UserDtoMapper ::new)
    .map(UserDtoMapper ::GetUserDto)

Leave a Reply

× 9 = sixty three

Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.