1

I have ArrayList and i want to get him some specific object values and insert them to another ArrayList, but I can't get the specific values

val listA = ArrayList<User>()

this list contains some users with their id,name, and email so i need to get all user's whose names are john and id,email will be any value

2
  • listA.filter { it.name == "john" } Commented Dec 29, 2019 at 21:36
  • Thank you, please post it as an answer to help the others Commented Dec 29, 2019 at 22:09

1 Answer 1

4

You could use the filter function i.e

listA.filter { it.name == "john" }

or use groupBy with forEach i.e

listA.groupBy{ it.name }.forEach{name, list ->

//name: the key for each list according to their names
//list: a grouped list according to the names

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the groupBy is saves me, the second solution

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.