How to return member values in a array of objects using lambda expressions

允我心安 提交于 2019-12-19 04:21:13

问题


I have an array of "Dog" where i want to print the name of all dogs older then 5 years.

I tried something like

Dogs.filter{ it.age > 5 }.forEach { it.name }

This gives me the value i need, but how do I store and return it as a list of strings? I tried things like adding .join(",") but since I don't get any array in return it wont work.


回答1:


I think you're looking for the map operator:

val dogNames: List<String> = dogs.filter { it.age > 5 }.map { it.name }


来源:https://stackoverflow.com/questions/49427606/how-to-return-member-values-in-a-array-of-objects-using-lambda-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!