简体   繁体   English

简化/迁移源代码方法到 Java 8

[英]Simplifying/Migrating Source Code method to Java 8

I have been attempting to convert this source code to be conventionally formatted using Java 8. I have been following a few conversion guides but haven't been able to get any solutions working.我一直在尝试将此源代码转换为使用 Java 8 的常规格式。我一直在遵循一些转换指南,但无法获得任何解决方案。 What am I doing wrong?我究竟做错了什么?

Here is the source code:这是源代码:

private List<ResponseVo> populateResponse(String userId, String type) {

    List<Wrapper> recordsList = logRepo.getEntities(type, userId);
    List<ResponseVo> responseVoList = new ArrayList<>();

    if (null != userId) {
        for (Wrapper record : recordsList) {
            if (record.getSortKey().contains(userId)) {
                ResponseVo responseVo = buildResultsResponse(record);
                responseVoList.add(responseVo);
            }
        }
    } else {
        for (Wrapper record : recordsList) {
            ResponseVo testLogResponseVo = buildResultsResponse(record);
            responseVoList.add(responseVo);
        }
    }
    return responseVoList;
}

I have attempted using the Collectors class and Stream mapping, but am having no luck.我曾尝试使用收集器 class 和 Stream 映射,但没有运气。 It looked something like this:它看起来像这样:

            recordsList.stream()
                .filter(record -> record.getSortKey().contains(userId))
                .collect(Collectors.toList());

Firstly, you need to provide the alternative response object for the case when userId is null .首先,您需要为userIdnull For that, you need to use the same conditional logic (it's not a task for streams).为此,您需要使用相同的条件逻辑(这不是流的任务)。

In the stream, you need to transform each Wrapper object the matches the predicate to your target type.在 stream 中,您需要将每个Wrapper object 匹配谓词转换为您的目标类型。 Which can be done using map() operation.这可以使用map()操作来完成。

private List<ResponseVo> populateResponse(String userId, String type) {
    
    if (userId == null) {
        return Collections.singletonList(buildResultsResponse(record));
    }
    
    return logRepo.getEntities(type, userId).stream()
        .filter(record -> record.getSortKey().contains(userId)) // Stream<Wrapper>
        .map(record -> buildResultsResponse(record))            // Stream<ResponseVo>
        .toList();                                              // for Java 16+ or collect(Collectors.toList())
}

It is possible with a single chain of calls starting with the Optional wrapper around the userId .userId周围的Optional包装器开始的单个调用链是可能的。

final List<ResponseVo> responseVoList = Optional
    .ofNullable(userId)                                // nullable userId
    .map(id -> recordsList.stream()                    // if non-null
            .filter(r -> r.getSortKey().contains(id))) // ... filter some out
    .orElseGet(recordsList::stream)                    // or else use all
    .map(this::buildResultsResponse)                   // build the response
    .collect(Collectors.toList());                     // pack as a List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM