根据 Java 8 中键的部分对映射键进行分组
我有一个 地图 ,其中包含以下信息:
"source.temp.temp1.issuer.value": "Hend",
"source.temp.temp2.issuer.value": "Ledge",
"source.temp.temp1.issuer.audence": "Legos1",
"source.temp.temp2.issuer.audence": "Legos2",
"source.temp.temp1.issuer.algp": "esc",
"source.temp.temp2.issuer.algp": "esc"
我需要根据密钥的一部分进行分组,即temp1
,temp2
等。保存它们并从中制作 list 。因此,最终结果应该是这样的:
[
{
"source.temp.temp1.issuer.value": "Hend",
"source.temp.temp1.issuer.audence": "Legos1",
"source.temp.temp1.issuer.algp": "esc"
},
{
"source.temp.temp2.issuer.value": "Ledge",
"source.temp.temp2.issuer.audence": "Legos2",
"source.temp.temp2.issuer.algp": "esc"
},
...
]
键是对象
类型。
I have a map which contains information as below:
"source.temp.temp1.issuer.value": "Hend",
"source.temp.temp2.issuer.value": "Ledge",
"source.temp.temp1.issuer.audence": "Legos1",
"source.temp.temp2.issuer.audence": "Legos2",
"source.temp.temp1.issuer.algp": "esc",
"source.temp.temp2.issuer.algp": "esc"
I need to do grouping based on the part of the key, i.e temp1
, temp2
, etc. Save them and make a list out of it. So the final result should be like this:
[
{
"source.temp.temp1.issuer.value": "Hend",
"source.temp.temp1.issuer.audence": "Legos1",
"source.temp.temp1.issuer.algp": "esc"
},
{
"source.temp.temp2.issuer.value": "Ledge",
"source.temp.temp2.issuer.audence": "Legos2",
"source.temp.temp2.issuer.algp": "esc"
},
...
]
The keys are of Object
type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用Stream API,您可以通过使用Collector
分类器
需要传递的函数,因为参数将用空字符串替换“ dessue”
之后的所有内容。默认情况下,groupingby()
将所有映射到同一键的元素收集到列表中。输出
With Stream API, you can do it by utilizing the collector
Collectors.groupingBy()
.The
classifier
function that needs to be passed as an argument will replace everything after"issue"
with an empty string. By default,groupingBy()
collects all elements mapped to the same key into a list.Output
您可以按照此操作,在每个步骤中添加评论。
在 gropingby 中,您可以随意更改登录名。目前,我用 temp1 &amp进行了硬编码。 temp2 。
You can follow this, added comments in every steps.
In groupingBy you can change the login whatever you want. Currently I hardcoded with temp1 & temp2 alone.