简体   繁体   English

Java 流:返回结果给自定义 object

[英]Java streams: return results to custom object

I have a piece of code that has list of objects as follows.我有一段代码,其中包含如下对象列表。

List<PivotMapEgModel> pivotMapList = List.of(new PivotMapEgModel(1L, "1"), new PivotMapEgModel(1L, "2"), new PivotMapEgModel(1L, "3"), new PivotMapEgModel(2L, "5"));

It is guaranteed that there will always be a maximum of 3 codes per value.保证每个值始终最多有 3 个代码。

I have a class that looks like this:我有一个 class,看起来像这样:

@Data
@AllArgsConstructor
class ResultSet {
    long value;
    String code_1;
    String code_2;
    String code_3;
}

I am currently doing the stream operation in this way:我目前正在以这种方式进行 stream 操作:

pivotMapList.stream().collect(Collectors.groupingBy(PivotMapEgModel::getValue, Collectors.mapping(PivotMapEgModel::getCode, Collectors.toList())))

This is producing the output in the following way: {1=[1, 2, 3], 2=[5]}这是通过以下方式生成 output: {1=[1, 2, 3], 2=[5]}

I need to perform stream operations on the pivotMapList to get the output to show in List<ResultSet> as follows:我需要对 pivotMapList 执行 stream 操作,以获取 output 以显示在List<ResultSet>中,如下所示:

[{value=1, code_1=1, code_2=2, code_3=3},
 {value=2, code_1=1, code_2=null, code_3=null}]

I am not sure how I can get List<ResultSet> from stream operations我不确定如何从 stream 操作中获取List<ResultSet>

Any help to achieve the desired output would be greatly appreciated!任何帮助实现所需的 output 将不胜感激! Thank you谢谢

You have already mapped value to its codes.您已经将值映射到它的代码。 You can just continue by streaming the entry set of the resulting map and map entries to ResultSet .您可以继续将生成的 map 和map条目的条目集流式传输到ResultSet

List<ResultSet> result = pivotMapList.stream()
            .collect(Collectors.groupingBy(PivotMapEgModel::getValue, Collectors.mapping(PivotMapEgModel::getCode, Collectors.toList())))
            .entrySet()
            .stream()
            .map(entry -> new ResultSet(entry.getKey(), getCode(entry.getValue(), 0), getCode(entry.getValue(), 1), getCode(entry.getValue(), 2)))
            .collect(Collectors.toList());

getCode() is simple method taking care not to get exception when retrieving values from the list. getCode()是一种简单的方法,在从列表中检索值时注意不要出现异常。

private static String getCode(List<String> codes, int index) {
  if (index >= codes.size()) {
    return null;
  }
  return codes.get(index);
}

Here is one way.这是一种方法。

class ResultSet {

    long value;
    String code_1;
    String code_2;
    String code_3;

    public ResultSet(long value, String code_1,String code_2, String code_3) {
        this.value = value;
        this.code_1 = code_1;
        this.code_2 = code_2;
        this.code_3 = code_3;
    }
    
    public String toString() {
        return "{%d, %s, %s, %s}".formatted(value, code_1, code_2, code_3);
    }
}

Use your existing map to build the list.使用您现有的 map 来构建列表。 Stream the entrySet of the map and use map to instantiate a ResultSet instance. Stream是map的entrySet,并使用map来实例化一个ResultSet实例。 The forloop will fill the list with nulls if it isn't fully populated.如果没有完全填充,forloop 将用空值填充列表。


List<ResultSet> resultSet = map.entrySet().stream()
        .<ResultSet>map(ent-> {
            List<String> lst = ent.getValue();
            for( int i = lst.size(); i < 3; i++) {
                lst.add(null);
            }
            return new ResultSet(ent.getKey(), lst.get(0),
                    lst.get(1), lst.get(2));
        }).toList();


resultSet.forEach(System.out::println);

prints印刷

{1, 1, 2, 3}
{2, 5, null, null}

Note that you could simply stream the existing entrySet from the original map to combine the process, returning the desired List<ResultSet>请注意,您可以简单地将原始 map 中的现有entrySet stream 组合起来,返回所需的List<ResultSet>

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

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