如何转换映射< string,map< string,string>>串

发布于 2025-02-05 06:20:56 字数 1618 浏览 1 评论 0原文

我有一个实体

public class Report {
    private String departmentName;
    private BigDecimal amount;
    private String currency;
}

,我想根据货币计算每个部门的金额,并将结果写入行。 示例:

    List<Report> list = new ArrayList<>();
    list.add(new Report("Moscow", new BigDecimal(100), "USD"));
    list.add(new Report("Moscow", new BigDecimal(100), "USD"));
    list.add(new Report("Paris", new BigDecimal(100), "USD"));
    list.add(new Report("Moscow", new BigDecimal(200), "RUR"));

结果:“莫斯科200 USD,巴黎100 USD,莫斯科200 rur”

为此:

Map<String, Map<String, String>> collect = list.stream().collect(
            Collectors.groupingBy(doc -> doc.getDepartmentName(),
                    Collectors.groupingBy(Report::getCurrency,
                            Collectors.collectingAndThen(Collectors.toList(), doc -> {
                                BigDecimal sum = doc.stream()
                                        .map(A::getAmount)
                                        .filter(Objects::nonNull)
                                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                                String cur = doc.stream().map(Report::getCurrency)
                                        .findFirst().get();
                                return sum.toString() + " " + cur;
                            }))));

我收到结果:

{Paris={USD=100 USD}, Moscow={USD=200 USD, RUR= 200 RUR}} -- correct

但是我不知道如何转换Map&lt; string; string,map&lt; string,字符串&gt;&gt;到字符串

I have an entity

public class Report {
    private String departmentName;
    private BigDecimal amount;
    private String currency;
}

and I want to calculate the amount for each department depending on the currency and write the result in the line.
Example:

    List<Report> list = new ArrayList<>();
    list.add(new Report("Moscow", new BigDecimal(100), "USD"));
    list.add(new Report("Moscow", new BigDecimal(100), "USD"));
    list.add(new Report("Paris", new BigDecimal(100), "USD"));
    list.add(new Report("Moscow", new BigDecimal(200), "RUR"));

Result: "Moscow 200 usd, Paris 100 USD, Moscow 200 RUR"

For it:

Map<String, Map<String, String>> collect = list.stream().collect(
            Collectors.groupingBy(doc -> doc.getDepartmentName(),
                    Collectors.groupingBy(Report::getCurrency,
                            Collectors.collectingAndThen(Collectors.toList(), doc -> {
                                BigDecimal sum = doc.stream()
                                        .map(A::getAmount)
                                        .filter(Objects::nonNull)
                                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                                String cur = doc.stream().map(Report::getCurrency)
                                        .findFirst().get();
                                return sum.toString() + " " + cur;
                            }))));

And I receive a result:

{Paris={USD=100 USD}, Moscow={USD=200 USD, RUR= 200 RUR}} -- correct

but I don't know how to convert Map<String, Map<String, String>> to String

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

阿楠 2025-02-12 06:20:56

所需的结果是“莫斯科200 USD,巴黎100 USD,Moscow 200 RUR”
使用Flatmap这样的尝试。

  • 流式传输主地图的入口集。
  • 通过入口集将内部地图的值置换。
  • 使用外映射的键和内部的值,将它们映射到字符串。
  • 然后与收藏家一起加入字符串。
String result = map.entrySet().stream()
        .flatMap(e -> e.getValue().entrySet().stream()
                .map(e2 -> e.getKey() + " " + e2.getValue()))
        .collect(Collectors.joining(", "));
System.out.println(result);

打印

Paris 100 USD, Moscow 200 RUR, Moscow 200 USD

注意:由于地图是无序的,因此根据总和,其他报告等,元素的顺序可能不一致。

我不知道您的地图是否是按原样的,还是获得结果的方法,但是您也可以这样做。

  • 像以前一样使用组,但请使用Tomap作为收藏家。
Map<String, Map<String, BigDecimal>> map =
        list.stream()
                .collect(Collectors.groupingBy(
                        Report::getDepartmentName,
                        Collectors.toMap(Report::getCurrency,
                                Report::getAmount, BigDecimal::add)));
  • 然后,您将进行类似于以前的操作,但在此处将BigDecimal转换为字符串,并将货币类型(e2.getKey())附加到其中。
String result = map1.entrySet().stream().flatMap(e -> e
                .getValue().entrySet().stream()
                .map(e2 -> e.getKey() + " " + e2.getValue().toString()
                        + " " + e2.getKey()))
                .collect(Collectors.joining(", "));

Desired result is "Moscow 200 usd, Paris 100 USD, Moscow 200 RUR"
Try it like this using flatMap.

  • stream the entrySet of the main map.
  • flatMap the inner map's values via entrySet again.
  • the using the key of the outer map and the value of the inner, map them to a string.
  • Then join the strings with a collector.
String result = map.entrySet().stream()
        .flatMap(e -> e.getValue().entrySet().stream()
                .map(e2 -> e.getKey() + " " + e2.getValue()))
        .collect(Collectors.joining(", "));
System.out.println(result);

prints

Paris 100 USD, Moscow 200 RUR, Moscow 200 USD

Note: Since maps are unordered, the order of the elements may not be consistent from run to run based on sums, additional Reports, etc.

I don't know if your Map is required as is or a way to get the result, but you could also do it like this.

  • use groupingBy as before but use toMap as the collector.
Map<String, Map<String, BigDecimal>> map =
        list.stream()
                .collect(Collectors.groupingBy(
                        Report::getDepartmentName,
                        Collectors.toMap(Report::getCurrency,
                                Report::getAmount, BigDecimal::add)));
  • Then you would do it similar to before but convert the BigDecimal to a String here and append the currency type(e2.getKey()) to that.
String result = map1.entrySet().stream().flatMap(e -> e
                .getValue().entrySet().stream()
                .map(e2 -> e.getKey() + " " + e2.getValue().toString()
                        + " " + e2.getKey()))
                .collect(Collectors.joining(", "));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文