假设我有一个List<Tuple>
,其中每个元素的第一个元素是一个字符串.Kotlin中是否有一个扩展函数可以从这些元组中 Select 第一个元素?
我正在寻找类似于Select
的C#LINQ语法:
myTuples.Select(t => t.item1)
假设我有一个List<Tuple>
,其中每个元素的第一个元素是一个字符串.Kotlin中是否有一个扩展函数可以从这些元组中 Select 第一个元素?
我正在寻找类似于Select
的C#LINQ语法:
myTuples.Select(t => t.item1)
In Kotlin, a Tuple could be a Pair
or a Triple
. You could just map over the list and select out the first element, like this:
val myTuples : List<Triple<String,String,String>> = listOf(
Triple("A", "B", "C"),
Triple("D", "E", "F")
)
val myFirstElements: List<String> = myTuples.map { it.first } // ["A", "D"]
And of course, you can leave off the types, I've left them in to make this easier to follow.