沿途转置收集钥匙的地图 [英] transpose a map collecting keys along the way

查看:78
本文介绍了沿途转置收集钥匙的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对地图进行转置,以便:

I'm trying to transpose a map so that:

[x: y, w: y, a: b]

成为

[y: [x, w], b: a]

(所有变量都是字符串)做类似的事情

(all variables are strings) Doing something like

["x": "y", "w": "y", "a": "b"].collectEntries { [it.value, it.key] }

让我有所作为,但着"y"的第一个新值.我只会得到:[y:w,b:a]

gets me part way, but stomps on the first new value for "y". I only get: [y:w, b:a]

将新值包含在它们的公共新键的数组中的最佳方法是什么?感谢您的帮助或建议.

What is the best way to slurp up the new values into an array for their common new key? Thanks for any help or suggestions.

推荐答案

我希望这会有所帮助:

def map = ["x": "y", "w": "y", "a": "b"]

map.groupBy{ it.value }.collectEntries{ k, v -> [k, v.keySet()] }

这篇关于沿途转置收集钥匙的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆