从列表中的每个对象中选择属性

时间:2017-09-12 15:45:22

标签: collections kotlin

假设我有一个List<Tuple>,其中每个元素中的第一个元素是一个字符串。在Kotlin中是否有一个扩展函数来从这些元组中选择第一个元素?

我正在寻找类似Select的C#LINQ语法:

myTuples.Select(t => t.item1)

1 个答案:

答案 0 :(得分:5)

在Kotlin中,元组可以是apiVersion: apps/v1beta1 kind: Deployment metadata: name: zalenium-deployment spec: selector: matchLabels: app: zalenium role: grid replicas: 1 template: metadata: labels: app: zalenium role: grid spec: serviceAccountName: zalenium serviceAccount: zalenium containers: - name: zalenium-pod image: dosel/zalenium ports: - containerPort: 4444 protocol: TCP volumeMounts: - name: zalenium-shared mountPath: /tmp/mounted - name: zalenium-videos mountPath: /home/seluser/videos resources: requests: memory: "250m" cpu: "500m" limits: memory: "1Gi" volumes: - name: zalenium-shared persistentVolumeClaim: claimName: zalenium-shared-claim - name: zalenium-videos persistentVolumeClaim: claimName: zalenium-videos-claim Pair。您只需映射列表并选择第一个元素,如下所示:

Triple

当然,你可以省去这些类型,我已将它们留下来,以便更容易理解。