根据 Java 8 中键的部分对映射键进行分组

发布于 2025-01-19 02:04:40 字数 852 浏览 0 评论 0原文

我有一个 地图 ,其中包含以下信息:

"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"

我需要根据密钥的一部分进行分组,即temp1temp2等。保存它们并从中制作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2025-01-26 02:04:40

使用Stream API,您可以通过使用Collector

分类器需要传递的函数,因为参数将用空字符串替换“ dessue”之后的所有内容。默认情况下,groupingby()将所有映射到同一键的元素收集到列表中。

Map<String, String> source =
         Map.of("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");

Map<Object, List<String>> result =
        source.keySet().stream()
              .collect(Collectors.groupingBy(str -> str.replaceAll("\\.issuer(.)*", "")));

result.entrySet().forEach(System.out::println);

输出

source.temp.temp2=[source.temp.temp2.issuer.value, source.temp.temp2.issuer.algp, source.temp.temp2.issuer.audence]
source.temp.temp1=[source.temp.temp1.issuer.algp, source.temp.temp1.issuer.audence, source.temp.temp1.issuer.value]

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.

Map<String, String> source =
         Map.of("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");

Map<Object, List<String>> result =
        source.keySet().stream()
              .collect(Collectors.groupingBy(str -> str.replaceAll("\\.issuer(.)*", "")));

result.entrySet().forEach(System.out::println);

Output

source.temp.temp2=[source.temp.temp2.issuer.value, source.temp.temp2.issuer.algp, source.temp.temp2.issuer.audence]
source.temp.temp1=[source.temp.temp1.issuer.algp, source.temp.temp1.issuer.audence, source.temp.temp1.issuer.value]
旧梦荧光笔 2025-01-26 02:04:40

您可以按照此操作,在每个步骤中添加评论。

        List<Map<String, String>> groups = source.entrySet().stream() //take maps entryset stream
            .collect(Collectors.groupingBy(e->e.getKey().contains("temp1")?"tempKey1":"tempKey2")) //group based on your term/logic Result will be <newKey, List<Entry>>
            .values().stream() //Ignore newly created key take values alone into stream again => result stream of Entry
            .map(entryList-> {
                return entryList.stream().collect(Collectors.toMap(e->e.getKey(), e->e.getValue()));
            }) //Convert entry into Map
            .collect(Collectors.toList()); //collect stream of map into List of map
    System.out.println(groups);

gropingby 中,您可以随意更改登录名。目前,我用 temp1 &amp进行了硬编码。 temp2

You can follow this, added comments in every steps.

        List<Map<String, String>> groups = source.entrySet().stream() //take maps entryset stream
            .collect(Collectors.groupingBy(e->e.getKey().contains("temp1")?"tempKey1":"tempKey2")) //group based on your term/logic Result will be <newKey, List<Entry>>
            .values().stream() //Ignore newly created key take values alone into stream again => result stream of Entry
            .map(entryList-> {
                return entryList.stream().collect(Collectors.toMap(e->e.getKey(), e->e.getValue()));
            }) //Convert entry into Map
            .collect(Collectors.toList()); //collect stream of map into List of map
    System.out.println(groups);

In groupingBy you can change the login whatever you want. Currently I hardcoded with temp1 & temp2 alone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文