首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java流响应子对象

如何使用java流响应子对象
EN

Stack Overflow用户
提问于 2022-08-22 17:14:41
回答 1查看 44关注 0票数 0

好吧,我一整天都在胡思乱想,解决不了这个问题。我正试图在对象列表中找到一个id,该id与java流类似于层次结构中的3层。我知道如何使用for循环来实现它,但是我需要使用流来实现它。json响应是

代码语言:javascript
复制
"NumberOfOwners": 1,
    "CurrentPage": 1,
    "TotalItems": 1,
    "TotalPages": 1,
    "PageSize": 1,
    "PageItems": [
        {
            "Id": 1560,
            "Title": "PlsWrk",
            "IsSubmitted": true,
            "Owner": {
                "Branch": null,
                "Position": null,
                "Id": null,
                "FirstName": null,
                "LastName": null,
                "ParentName": null,
                "Gender": null,
                "Image": null,
                "LoginStatusId": 0,
                "EmployeeStatus": 0,
                "CompanyName": null,
                "IsSubcontractor": false
            },
            "KeyResults": [
                {
                    "Id": 5032,
                    "Title": "asdf1",
                    "OverallStatus": 2,
                    "MonthKeyResults": [
                        {
                            "Id": 12484,
                            "Month": 9,
                            "Status": 3,
                            "Progress": "REaplace1"
                        },
                        {
                            "Id": 12483,
                            "Month": 8,
                            "Status": 3,
                            "Progress": "sadf4"
                        },
                        {
                            "Id": 12482,
                            "Month": 7,
                            "Status": 1,
                            "Progress": "On Track1"
                        }
                    ]
                },
                {
                    "Id": 5033,
                    "Title": "asdf2",
                    "OverallStatus": 1,
                    "MonthKeyResults": [
                        {
                            "Id": 12485,
                            "Month": 7,
                            "Status": 2,
                            "Progress": "Recovery2"
                        },
                        {
                            "Id": 12487,
                            "Month": 9,
                            "Status": 2,
                            "Progress": "asdfreas"
                        },
                        {
                            "Id": 12486,
                            "Month": 8,
                            "Status": 1,
                            "Progress": "asdf5"
                        }
                    ]
                },
                {
                    "Id": 5034,
                    "Title": "asdf3",
                    "OverallStatus": 2,
                    "MonthKeyResults": [
                        {
                            "Id": 12490,
                            "Month": 9,
                            "Status": 1,
                            "Progress": "asdafa"
                        },
                        {
                            "Id": 12489,
                            "Month": 8,
                            "Status": 2,
                            "Progress": "asdf6"
                        },
                        {
                            "Id": 12488,
                            "Month": 7,
                            "Status": 3,
                            "Progress": "Replace3"
                        }
                    ]
                }
            ]
        }
    ]

确切地说,我希望流返回具有特定id Atm的MonthyKeyResult对象。

代码语言:javascript
复制
public static MonthKeyResults getOkrMonthlyProgressById(PageItems okr, Integer monthlyProgressId){

        //here i get KeyResults object list
        List<KeyResults> keyResult = okr.getKeyResults().stream().collect(Collectors.toList());
            
            //and now I am trying to get all MonthKeyResults 
            //objects but I not doing it right
            List<MonthKeyResults> monthKeyResults = keyResult.stream().
                       filter(monthKeyResult -> monthKeyResult.
                                  getMonthKeyResults().
                                  stream().collect(Collectors.toList()));
            
            //and then I am thinking of going trough the monthKeyResults 
            //list with stream and finding Id I need and returning that 
            //whole object with something like this
    MonthKeyResults mKeyResults = monthKeyResults.stream().filter(id -> id.getId().
                    equals(monthlyProgressId)).findAny().orElse(null);
    return mKeyResult
}

我还有一个问题--我已经把最后一个对象分开了--就像你在3条流中看到的那样,你能一次得到它吗?还是你需要像这样把这些对象分开,然后把它们分开处理?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-22 17:29:19

也许你在找的东西是:

代码语言:javascript
复制
return okr.getKeyResults().stream()
        .flatMap(keyResult -> keyResult.getMonthKeyResults().stream())
        .filter(monthKeyResult -> monthKeyResult.getId().equals(monthlyProgressId))
        .findAny()
        .orElse(null);

编辑:修正错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73448783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档