首页 > 解决方案 > 使用流将一个类对象转换为另一个

问题描述

我知道这个问题可能会产生误导,如果可能有人可以纠正它,我不知道如何在这种情况下提出问题。

我正在尝试将 X 类转换为 Y 类(这里 Y 类包含 X 类的字段,但以不同的方式,例如:-Integer a, b;在 X 类内部,转换为Map<a, b>Y 类,而其他变量保持不变。),

在java 8中使用流。我试图将最终对象作为json返回到UI部分。该项目是在spring boot中完成的。X 类和 Y 类都包含相同的对象,但 Y 类是为了使 X 类与众不同我不熟悉流。

X 级

public class X {

private final String thumbnailUrl;
private final Integer duration;
private final String contentId;
private final Date reportDate;
private final Integer count;

public X(Report report, int count) {
    this.thumbnailUrl = report.getContent().getThumbnailUrl();
    this.duration = report.getContent().getDuration();
    this.contentId = report.getContent().getContentId();
    this.reportDate = report.getReportDate();
    this.count = count;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}

public Integer getDuration() {
    return duration;
}

public String getContentId() {
    return contentId;
}

public Date getReportDate() {
    return reportDate;
}

public Integer getCount() {
    return count;
}    

}

Y 类

public class Y {

private final String thumbnailUrl;
private final Integer duration;
private final String contentId;
private final Map<Date, Integer> contentList;

public Y(X x, Map<Date, Integer> contentList) {
    this.thumbnailUrl = x.getThumbnailUrl();
    this.duration = x.getDuration();
    this.contentId = x.getContentId();
    this.contentList = contentList;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}

public Integer getDuration() {
    return duration;
}

public String getContentId() {
    return contentId;
}

public Map<Date, Integer> getContentList() {
    return contentList;
}

}

这是我目前从 X 班得到的

[
{
    "thumbnailUrl": "a",
    "duration": 12,
    "contentId": "CNT10",
    "reportDate": "2020-01-20",
    "count": 3
},
{
    "thumbnailUrl": "a",
    "duration": 12,
    "contentId": "CNT10",
    "reportDate": "2020-01-21",
    "count": 5
},
{
    "thumbnailUrl": "a",
    "duration": 12,
    "contentId": "CNT10",
    "reportDate": "2020-01-22",
    "count": 3
},
{
    "thumbnailUrl": "a",
    "duration": 12,
    "contentId": "CNT10",
    "reportDate": "2020-01-23",
    "count": 4
}
]

使用此代码并返回最终列表后,我在邮递员中获得了上述 json。

List<X> x;
List<Y> y;

x = StreamSupport.stream(reportRepository
                .reportWithRoll(a, b, c).spliterator(), false)
                .map(report -> new X(report, report.getStartCount()))
                .collect(Collectors.toList());

我希望将其转换为 Y 类的内容,如下所示。如何使用 Java 中的流来实现这一点?

[
    {
        "list": [
            {
                "2020-01-20": 3,
                "2020-01-21": 5,
                "2020-01-22": 3,
                "2020-01-23": 4
            }
        ],
        "thumbnailUrl": "a",
        "duration": 12,
        "contentId": "CNT10"
    }
]

我试过这个来获取上面的 json 格式,但最终得到了重复的数据,单个 contentId 和多个 contentId 的错误

y = x.stream().map(
                rep -> {
                    Map<Date, Integer> contentList = x.stream().collect(
                            Collectors.toMap(X::getReportDate, X::getCount)
                    );
                    Y yy = new Y(rep, contentList);
                    return yy;
                }
        ).distinct().collect(Collectors.toList());

我将公共日期和计数:键值对组合成一个“列表”,用于每个唯一的“contentId”(每个唯一的“contentId”都有自己的“thumbnailUrl”和“duration”,所以当我指的是只有“contentId”是唯一的,它将包括“thumbnailUrl”和“duration”,每个日期和计数将是多个)。

标签: javajava-stream

解决方案


考虑到将为输入中的任何内容contentId带来相同的值,您可以分两步完成:thumbnailUrldurationX

Map<String, X> contentIdLookUp = x.stream()
        .collect(Collectors.toMap(X::getContentId, Function.identity()));

List<Y> y = x.stream()
        .collect(Collectors.groupingBy(X::getContentId, 
                Collectors.toMap(X::getReportDate, X::getCount))))
        .entrySet().stream()
        .map(e -> new Y(contentIdLookUp.get(e.getKey()), e.getValue()))
        .collect(Collectors.toList());

推荐阅读