Java Stream - >从另一个不同结构的列表创建列表

时间:2018-01-19 08:22:47

标签: java java-stream

我是Java Stream api的新手,非常感谢您的帮助。

我正在尝试将此结构(第一个结构)转换为

[(id=BNA, name=Nashville, TN, loadsCount=1, distance=null, metricScoresList=[BattingOrderResponse.MetricScoresList(identifier=BNA, name=null, aggregatedScore=35.5, onTimeToDestinationPercentage=0.0, onTimeToPickupPercentage=0.0, onTimeToPickupDepartPercentage=0.0, tenderAcceptancePercentage=18.2, tenderCancellationPercentage=0.0, appUsageCompliancePercentage=100.0, revenue=0.0, loadsCount=1, distance=0.0)])...]

这是班级模特:

@ToString
@Getter
public final class BattingOrderResponse {

private final String id;
private final String name;
private final long loadsCount;
private final Distance distance;
private final List<MetricScores> metricScores;
@JsonCreator
public BattingOrderResponse(@JsonProperty("id") final String id,
        @JsonProperty("name") final String name,
        @JsonProperty("loadsCount") final long loadsCount,
        @JsonProperty("distance") final Distance distance,
        @JsonProperty("metricScores") final List<MetricScores> metricScores) {
    this.id = id;
    this.name = name;
    this.loadsCount = loadsCount;
    this.distance = distance;
    this.metricScores = metricScores;
}

@ToString
@Getter
public static final class Distance {

    private final double value;
    private final String unit;
    @JsonCreator
    public Distance(@JsonProperty("value") final double value,
            @JsonProperty("unit") final String unit) {
        this.value = value;
        this.unit = unit;
    }

}

@ToString
@Getter
public static final class MetricScores {

    private final String identifier;
    private final String name;
    private final double aggregatedScore;
    private final double onTimeToDestinationPercentage;
    private final double onTimeToPickupPercentage;
    private final double onTimeToPickupDepartPercentage;
    private final double tenderAcceptancePercentage;
    private final double tenderCancellationPercentage;
    private final double appUsageCompliancePercentage;
    private final double revenue;
    private final long loadsCount;
    private final double distance;

    @JsonCreator
    //CHECKSTYLE:SUPPRESS:ParameterNumberCheck
    public MetricScores(@JsonProperty("identifier") final String identifier,
            @JsonProperty("name") final String name,
            @JsonProperty("aggregatedScore") final double aggregatedScore,
            @JsonProperty("onTimeToDestinationPercentage") final double onTimeToDestinationPercentage,
            @JsonProperty("onTimeToPickupPercentage") final double onTimeToPickupPercentage,
            @JsonProperty("onTimeToPickupDepartPercentage") final double onTimeToPickupDepartPercentage,
            @JsonProperty("tenderAcceptancePercentage") final double tenderAcceptancePercentage,
            @JsonProperty("tenderCancellationPercentage") final double tenderCancellationPercentage,
            @JsonProperty("appUsageCompliancePercentage") final double appUsageCompliancePercentage,
            @JsonProperty("revenue") final double revenue,
            @JsonProperty("loadsCount") final long loadsCount,
            @JsonProperty("distance") final double distance) {
        this.identifier = identifier;
        this.name = name;
        this.aggregatedScore = aggregatedScore;
        this.onTimeToDestinationPercentage = onTimeToDestinationPercentage;
        this.onTimeToPickupPercentage = onTimeToPickupPercentage;
        this.onTimeToPickupDepartPercentage = onTimeToPickupDepartPercentage;
        this.tenderAcceptancePercentage = tenderAcceptancePercentage;
        this.tenderCancellationPercentage = tenderCancellationPercentage;
        this.appUsageCompliancePercentage = appUsageCompliancePercentage;
        this.revenue = revenue;
        this.loadsCount = loadsCount;
        this.distance = distance;
    }

}
}

[(id=BNA, name=null, overallScore=35.5, onTimeScore=0.0, TenderAcceptanceScore=18.2, appUsageScore=100.0),...]

班级模特:

@Getter
@ToString
@Builder
public final class DomicileScore {

private final String id;

private final String name;

private final double overallScore;

private final double onTimeScore;

private final double TenderAcceptanceScore;

private final double appUsageScore;

}

提示:id是BattingOrderResponse.id       name是BattingOrderResponse.name       overallScore是BattinOrderResponse.MetricScore       其余全部来自BattinOrderResponse.MetricScore

应该是&#39; name = Nashville&#39;而不是&#39; name = null&#39; (已从metricScoresList中引用。

这是我尝试过的代码。

scoreByDomicile.stream()
            .peek(i -> System.out.println(i.getName()))
            .map(i -> i.getMetricScoresList().get(0))
            .map(i -> DomicileScore.builder()
                    .id(i.getIdentifier())
                    .name(i.getName())
                    .overallScore(i.getAggregatedScore())
                    .appUsageScore(i.getAppUsageCompliancePercentage())
                    .onTimeScore(i.getOnTimeToDestinationPercentage())
                    .TenderAcceptanceScore(i.getTenderAcceptancePercentage())
                    .build())
             .collect(Collectors.toList());

其中scoreByDomicile是我的第一个结构中的原始数据列表。我需要在build()中添加map中的peek结果(代码中的第二个map)。

我对此持续了几个小时并且无法解决它。如果您需要更多详细信息,请告诉我们。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要两个参考,因此您无法使用双重映射 试试这个:

scoreByDomicile.stream()
        .peek(i -> System.out.println(i.getName()))
        .map(obj -> {
              MetricScores i = obj.getMetricScoresList().get(0); // 
              return DomicileScore.builder()
                .id(i.getIdentifier())
                .name(obj.getName()) // obj has the original name
                .overallScore(i.getAggregatedScore())
                .appUsageScore(i.getAppUsageCompliancePercentage())
                .onTimeScore(i.getOnTimeToDestinationPercentage())
                .TenderAcceptanceScore(i.getTenderAcceptancePercentage())
                .build();
          })
         .collect(Collectors.toList());