1

I was wondering if there was a shortcut to do that with Kotlin:

val userNames = ArrayList<String>()

for (user in users)
  userNames.add(user.name)

return userNames

That would be great if there was a one-line way to do this. Am I dreaming?

Thank you

1 Answer 1

4

You're just performing a basic mapping operation. This is it:

users.map { it.name }
Sign up to request clarification or add additional context in comments.

2 Comments

Any idea if there is an equivalent with Java 8? I could not find one :/
You couldn't find Stream.map()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.