Java 8 lambda create list of Strings from list of custom class objects
Asked Answered
C

5

46

I have the following qustion:

How can I convert the following code snipped to Java 8 lambda style?

List<String> tmpAdresses = new ArrayList<String>();
for (User user : users) {
    tmpAdresses.add(user.getAdress());
}

Have no idea and started with the following:

List<String> tmpAdresses = users.stream().map((User user) -> user.getAdress());
Carrew answered 8/8, 2018 at 13:14 Comment(2)
You're almost there. Assuming your map lambda returns a String just add .collect(Collectors.toList()) to map(...).Dacosta
Note: (User user) -> user.getAddress() can be written as user -> user.getAddress() or just User::getAddressEscent
I
104

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 as well be written as user -> user.getAdress() (because the type User will be inferred by the compiler)

Inclinometer answered 8/8, 2018 at 13:15 Comment(0)
A
9

It is extended your idea:

List<String> tmpAdresses = users.stream().map(user ->user.getAdress())
.collect(Collectors.toList())
Airy answered 8/8, 2018 at 13:28 Comment(1)
You should add a new answer if its better/ alternative than the other one. Explain how its better.Succession
S
4

One more way of using lambda collectors like above answers

 List<String> tmpAdresses= users
                  .stream()
                  .collect(Collectors.mapping(User::getAddress, Collectors.toList()));
Saeger answered 8/8, 2018 at 13:49 Comment(0)
M
0

You are almost there, you just need to collect streams in a List.

 List<String> addressList =
  users
    .stream()
    .map(user -> user.getAddress())
    .collect(Collectors.toList())
Maladroit answered 5/10, 2023 at 23:27 Comment(4)
How is your answer different to the already existing ones posted 5 years ago?Inclinometer
I am new here, just want to contribute more and more. That's why I didn't check the timing of previous answers.Maladroit
You should check whether your answer is different from other answers, we don't want 50 answers to one question where all are saying the exact same thing. If you want to contribute I invite you to search for unanswered questions (e.g. for the java tag) directly.Inclinometer
Okay, thanks @Lino. Will surely search for unanswered questions, and try to give solutions.Maladroit
K
-3

You can use this to convert Long to String:

List<String> ids = allListIDs.stream()
                             .map(listid-> String.valueOf(listid))
                             .collect(Collectors.toList());
Khorma answered 22/10, 2020 at 9:5 Comment(1)
This doesn't attempt to answer the OPs question, though. OP is explicitly asking to map a User to its adress. Also how is your question different to the already existing ones?Inclinometer

© 2022 - 2024 — McMap. All rights reserved.