我有以下列表
val listA = List(("tcp",22,"All","sshd"), ("tcp6",22,"All","sshd"), ("tcp6",443,"All","docker-proxy"), ("tcp6",8000,"All","docker-proxy"), ("tcp6",4100,"All","docker-proxy"), ("tcp6",5000,"All","docker-proxy"), ("tcp",5000,"All","docker-proxy"),("tcp6",4200,"All","docker-proxy"), ("tcp6",80,"All","docker-proxy"))
listA
的类型为List[(String, Int, String, String)]
,我的预期输出为
val output = List(("tcp",22,"All","sshd"), ("tcp6",443,"All","docker-proxy"), ("tcp6",8000,"All","docker-proxy"), ("tcp6",4100,"All","docker-proxy"), ("tcp6",5000,"All","docker-proxy"), ("tcp6",4200,"All","docker-proxy"), ("tcp6",80,"All","docker-proxy"))
这里想要匹配listA.map(_._2) distinct
我试过
val output = listA.groupBy(_._2).map {
case (key, value) =>
if (value.map(_._2).contains(key)) {
value
}
}
但上面没有给出我预期的结果(它与listA相同)。
如何获得任何人都知道的预期输出?
答案 0 :(得分:1)
如果我理解你想要达到的目标,那就是你需要的
listA.groupBy(_._2).map{ case (_, value) => value.head }
它从每个端口组中选择第一个元素。