Java 8 - 如何将数组List转换为特定类对象的List

时间:2021-11-01 21:27:38

Using the Java 8 Stream package I want to transform a List of arrays of type object into a List of a specific class object. The arrays on the first list contain field of the class loaded from the database.

使用Java 8 Stream包我想将类型为object的数组List转换为特定类对象的List。第一个列表中的数组包含从数据库加载的类的字段。

This is the List of arrays of type object loaded from the DB:

这是从DB加载的类型对象的数组列表:

List<Object[]> results = loadFromDB();

Every element Object[] present in the list contains fields that I want to map to the following class:

列表中的每个元素Object []都包含我要映射到以下类的字段:

class DeviationRisk {
    Timestamp plannedStart;
    Timestamp plannedEnd;
    String rcsName;
    BigDecimal riskValue;
    BigDecimal mediumThreshold;
    BigDecimal highThreshold;
    Interval interval;

    String getRcsName() {
        return rcsName;
    }

    DeviationRisk(Object[] res) {
        this((Timestamp) res[0], (Timestamp) res[1], (String) res[2], (BigDecimal) res[3], (BigDecimal) res[4], (BigDecimal) res[5]);

    }

    DeviationRisk(Timestamp start, Timestamp end, String rcs, BigDecimal risk, BigDecimal medium, BigDecimal high) {
        plannedStart = start;
        plannedEnd = end;
        rcsName = rcs;
        riskValue = risk;
        mediumThreshold = medium;
        highThreshold = high;
        interval = new Interval(plannedStart.getTime(), plannedEnd.getTime());
    }

    DeviationRisk create(Object[] res) {
              return new DeviationRisk(res);
          }

    List<DateTime> getRange() {
        return DateUtil.datesBetween(new DateTime(plannedStart), new DateTime(plannedEnd));
    }
}

As you can see every element Object[] present in the original list results it's just the array representation of the object DeviationRisk

正如您所看到的,原始列表中的每个元素Object []都会导致它只是对象DeviationRisk的数组表示

Now, I know how to do this using loops it's just 3 lines of code as you can see below:

现在,我知道如何使用循环它只需3行代码,如下所示:

  List<DeviationRisk> deviations = new ArrayList<>();
        for (Object[] res : results) {
            deviations.add(new DeviationRisk(res));
        }

How to achieve the same result using Java 8 Streams?

如何使用Java 8 Streams实现相同的结果?

2 个解决方案

#1


18  

You can try with:

您可以尝试:

List<DeviationRisk> result = results.stream()
                                    .map(DeviationRisk::new)
                                    .collect(Collectors.toList())

#2


6  

This will do it:

这样做:

final List<DeviationRisk> l = results.stream()
    .map(DeviationRisk::new).collect(Collectors.toList());

This work because DeviationRisk::new is viewed as a Function<Object[], DeviationRisk> by the compiler.

这项工作是因为DeviationRisk :: new被编译器视为Function

#1


18  

You can try with:

您可以尝试:

List<DeviationRisk> result = results.stream()
                                    .map(DeviationRisk::new)
                                    .collect(Collectors.toList())

#2


6  

This will do it:

这样做:

final List<DeviationRisk> l = results.stream()
    .map(DeviationRisk::new).collect(Collectors.toList());

This work because DeviationRisk::new is viewed as a Function<Object[], DeviationRisk> by the compiler.

这项工作是因为DeviationRisk :: new被编译器视为Function