如何使用Lambda表达式返回对象数组中的成员值 [英] How to return member values in a array of objects using lambda expressions
本文介绍了如何使用Lambda表达式返回对象数组中的成员值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个狗"数组,我要在其中打印所有5岁以上的狗的名字.
I have an array of "Dog" where i want to print the name of all dogs older then 5 years.
我尝试过类似的事情
Dogs.filter{ it.age > 5 }.forEach { it.name }
这给了我所需的值,但是如何存储并以字符串列表的形式返回呢? 我尝试了添加.join(,")之类的事情,但是由于我没有得到任何数组作为回报,因此无法正常工作.
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.
推荐答案
I think you're looking for the map
operator:
val dogNames: List<String> = dogs.filter { it.age > 5 }.map { it.name }
这篇关于如何使用Lambda表达式返回对象数组中的成员值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文