使用流将一个类对象转换为另一个类对象 [英] convert one class object into another using stream

查看:309
本文介绍了使用流将一个类对象转换为另一个类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能会引起误解,如果可能有人可以纠正它,我不确定在这种情况下如何提出问题.

I know this question can be misleading, if possible someone may correct it, I am not sure how to ask a question with this situation.

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

I am trying to Convert Class X into Class Y (here class Y contains fields of class X, but in different way, Eg:- Integer a, b; inside class X , converts to Map<a, b> in class Y while other variables stay the same.),

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

using streams in java 8. I am trying to return the final object to the UI part as json. The project is done in spring boot. Both Class X and Y contains the same objects, but Class Y is to make class X distinct I am not familiar with streams.

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班获得的

This is what I am currently getting from class 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.

I got the above json in postman after using this code and returning the final list.

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流实现此目标?

I want this to transformed into content of Class Y as below. How can I achieve this with streams in 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错误

I tried this to get the above json format, but ended up getting duplicate data, for single contentId and error for multiple 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",其中每个的日期和计数都将是多个.)

I am combining the common date and count : key value pair into a single "list" for each unique "contentId" (each unique "contentId" will have its own "thumbnailUrl" and "duration" specific to it, so when I am referring only "contentId" as unique, it will include "thumbnailUrl" and "duration", only the date and count will be multiple for each of these).

推荐答案

由于堆栈溢出我得到了以下json

I got the following json with this

[
{
    "thumbnailUrl": "a",
    "duration": 12,
    "contentId": "CNT10",
    "contentList": {
        "2020-01-21T18:30:00.000+0000": 3,
        "2020-01-20T18:30:00.000+0000": 3,
        "2020-01-22T18:30:00.000+0000": 3,
        "2020-01-19T18:30:00.000+0000": 3
    }
},
{
    "thumbnailUrl": "b",
    "duration": 12,
    "contentId": "CNT12",
    "contentList": {
        "2020-01-19T18:30:00.000+0000": 3
    }
},
{
    "thumbnailUrl": "c",
    "duration": 12,
    "contentId": "CNT11",
    "contentList": {
        "2020-01-21T18:30:00.000+0000": 3,
        "2020-01-20T18:30:00.000+0000": 3,
        "2020-01-19T18:30:00.000+0000": 3
    }
}
]

这篇关于使用流将一个类对象转换为另一个类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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