我有一个自定义对象列表.以下是data class
:
data class ProductsResponse(
val id:String,
val ProductType:String
)
Run Code Online (Sandbox Code Playgroud)
我有一个下面的列表:
var productList: List<ProductResponse>
我希望输出为:
var productNameList: List<String>
我想其中包括一个列表只ProductType
,即一个list
的Strings
我知道使用for循环并将ProductType
字符串复制到新列表将完成这项工作.但我想用更短的方式使用电源Kotlin
提供.
我怎样才能在上面的自定义对象转换list
为list
的Strings
在科特林方式?
pix*_*ix4 19
您可以使用地图功能:
val productNameList:List<String> = productList.map { it.ProductType }
Run Code Online (Sandbox Code Playgroud)
这会将每个ProductsResponse映射到它的ProductType.
val productNameList = productList.map { it.ProductType }
Run Code Online (Sandbox Code Playgroud)
无需指定类型,它将被推断
归档时间: |
|
查看次数: |
4306 次 |
最近记录: |