我有下面的名单
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相同)。
如何获得预期的产量,谁都知道?
发布于 2015-05-06 11:16:05
如果我正确理解你想要达到的目标,这就是你所需要的。
listA.groupBy(_._2).map{ case (_, value) => value.head }它从每个端口组中选择第一个元素。
https://stackoverflow.com/questions/30074009
复制相似问题