Java 8 lambda create list of Strings from list of objects

We Are Going To Discuss About Java 8 lambda create list of Strings from list of objects. So lets Start this Java Article.

Java 8 lambda create list of Strings from list of objects

  1. Java 8 lambda create list of Strings from list of objects

    You need to collect your stream into a List:
    List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList());
    For more information on the different Collectors visit the documentation

  2. Java 8 lambda create list of Strings from list of objects

    You need to collect your stream into a List:
    List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList());
    For more information on the different Collectors visit the documentation

Solution 1

You need to collect your stream into a List:

List<String> adresses = users.stream()
    .map(User::getAdress)
    .collect(Collectors.toList());

For more information on the different Collectors visit the documentation

User::getAdress is just another form of writing (User user) -> user.getAdress() which could aswell be written as user -> user.getAdress() (because the type User will be inferred by the compiler)

Original Author Lino Of This Content

Solution 2

It is extended your idea:

List<String> tmpAdresses = users.stream().map(user ->user.getAdress())
.collect(Collectors.toList())

Original Author Piotr Rogowski Of This Content

Solution 3

One more way of using lambda collectors like above answers

 List<String> tmpAdresses= users
                  .stream()
                  .collect(Collectors.mapping(User::getAddress, Collectors.toList()));

Original Author Oomph Fortuity Of This Content

Solution 4

You can use this to convert Long to String:

List<String> ids = allListIDs.stream()
                             .map(listid-> String.valueOf(listid))
                             .collect(Collectors.toList());

Original Author Doron ben ezra Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment