使用Stream API进行集合过滤时出现问题。

huangapple go评论74阅读模式
英文:

Problem with filtering collection, using Stream API

问题

我尝试使用 Stream API 对具有集合字段的对象集合进行筛选:

@Override
public List<Flight> method(List<Flight> flightList) 
{
    List<Flight> trueFlights = flightList.stream()
        .filter(flight -> flight.getSegments().stream()
            .allMatch(segment -> segment.getArrivalDate().isBefore(segment.getDepartureDate())))
        .collect(Collectors.toList());
    return trueFlights;
}

但无法编译通过:

错误:(32, 27) java: 不兼容的类型: 无法将void转换为java.util.List<Flight>

Segment 类:

class Segment {
    private final LocalDateTime departureDate;
    private final LocalDateTime arrivalDate;
    // ..getter setter
}

Flight 类:

class Flight {
    private final List<Segment> segments;
    // ..getter setter
}

我做错了什么?

英文:

I try to filter collection of objects that have collection field, using Stream API:

@Override
public void method (List&lt;Flight&gt; flightList) 
{
List&lt;Flight&gt; trueFlights = flightList.forEach(flight -&gt; flight.getSegments().stream().filter(segment -&gt; segment.getArrivalDate().isBefore(segment.getDepartureDate())).collect(Collectors.toList()));
return trueFlights;
}

But it does not compile:

> Error:(32, 27) java: incompatible types: void cannot be converted to
> java.util.List<Flight>

Segment:

class Segment {
    private final LocalDateTime departureDate;

    private final LocalDateTime arrivalDate; 
    //..getter setter  

Flight:

class Flight {
    private final List&lt;Segment&gt; segments;
   //.. getter setter 

What am I doing wrong?

答案1

得分: 1

你可能在寻找对Stream执行的filter操作。这还需要您使用终端操作anyMatchallMatch来包含谓词(predicate)。

例如,如果您希望在任何一个航段满足到达日期早于出发日期的条件时,将输入航班包括在trueFlights中,您可以执行anyMatch

List<Flight> trueFlights = flightList.stream()
        .filter(flight -> flight.getSegments().stream()
                .anyMatch(segment -> segment.getArrivalDate().isBefore(segment.getDepartureDate())))
        .collect(Collectors.toList());

而您之前错误的做法是使用具有void返回类型的forEach,然后试图进一步从中收集元素。

英文:

You could be looking for filter operation over a Stream. That would further require you to include a predicate, which is possible with the use of terminal operations anyMatch, allMatch over the inner stream.

For example, if you want to include input flights to trueFlights if any of its segment satisfies the condition that the arrival date is before departure date, you could perform anyMatch:

List&lt;Flight&gt; trueFlights = flightList.stream()
        .filter(flight -&gt; flight.getSegments().stream()
                .anyMatch(segment -&gt; segment.getArrivalDate().isBefore(segment.getDepartureDate())))
        .collect(Collectors.toList());

What you did incorrectly on the other hand was to make use of forEach which has a void return type and then trying to collect elements further from it.

huangapple
  • 本文由 发表于 2020年9月26日 09:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64072965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定