用流收集中间数据

时间:2017-11-15 15:41:00

标签: java java-stream collectors

您需要阅读一些xml文件部分来收集一些数据并根据密钥进行一些分组。我曾尝试使用java 8流进行一次性操作,但我发现很难将中间数据和数据组合在一起。这就是我到目前为止所拥有的

<air:AirPricePointList>
    <air:AirPricePoint Key="5ScAI+BAAA" TotalPrice="" BasePrice="" ApproximateTotalPrice="" ApproximateBasePrice="" EquivalentBasePrice="" Taxes="" ApproximateTaxes="">
        <air:AirPricingInfo Key="" TotalPrice="" BasePrice="600.00" lasticketingTime="" PlatingCarrier="AR">
            <air:PassengerType Code="ADT" />
            <air:FlightOptionsList>
                <air:FlightOption LegRef="5ScAI+BAAA/BGNTVfDAAAA==" Destination="NQN" Origin="ROS">
                    <air:Option Key="5ScAI+BAAA/BSTTVfDAAAA==" TravelTime="P0DT14H20M0S">
                        <air:BookingInfo BookingCode="K" BookingCount="7" CabinClass="Economy" FareInfoRef="5ScAI+BAAA/BwMTVfDAAAA==" SegmentRef="5ScAI+BAAA/BSMTVfDAAAA==" />
                        <air:BookingInfo BookingCode="U" BookingCount="5" CabinClass="Economy" FareInfoRef="5ScAI+BAAA/B2MTVfDAAAA==" SegmentRef="5ScAI+BAAA/BUMTVfDAAAA==" />
                        <air:Connection SegmentIndex="0" />
                    </air:Option>
                </air:FlightOption>

                <air:FlightOption LegRef="5ScAI+BAAA/BKNTVfDAAAA==" Destination="ROS" Origin="NQN">
                    <air:Option Key="5ScAI+BAAA/BVTTVfDAAAA==" TravelTime="P0DT7H20M0S">
                        <air:BookingInfo BookingCode="U" BookingCount="7" CabinClass="Economy" FareInfoRef="5ScAI+BAAA/B3MTVfDAAAA==" SegmentRef="5ScAI+BAAA/BWMTVfDAAAA==" />
                        <air:BookingInfo BookingCode="K" BookingCount="7" CabinClass="Economy" FareInfoRef="5ScAI+BAAA/BgNTVfDAAAA==" SegmentRef="5ScAI+BAAA/BYMTVfDAAAA==" />
                        <air:Connection SegmentIndex="0" />
                    </air:Option>

                    <air:Option Key="5ScAI+BAAA/BYTTVfDAAAA==" TravelTime="P0DT17H0M0S">
                        <air:BookingInfo BookingCode="U" BookingCount="7" CabinClass="Economy" FareInfoRef="5ScAI+BAAA/B3MTVfDAAAA==" SegmentRef="5ScAI+BAAA/BWMTVfDAAAA==" />
                        <air:BookingInfo BookingCode="K" BookingCount="7" CabinClass="Economy" FareInfoRef="5ScAI+BAAA/BgNTVfDAAAA==" SegmentRef="5ScAI+BAAA/BaMTVfDAAAA==" />
                        <air:Connection SegmentIndex="0" />
                    </air:Option>
                </air:FlightOption>
            </air:FlightOptionsList>

        </air:AirPricingInfo>

和映射功能

final Map<String, List<Option>> segmentOptions =
 lowFareSearchRsp.getAirPricePointList().getAirPricePoint().stream()                 
 .flatMap(airPricePoint -> 
     airPricePoint.getAirPricingInfo().stream()
 )
 .filter(this::isAdultAirPricingInfo)
 .flatMap(airPricingInfo ->  
    airPricingInfo.getFlightOptionsList().getFlightOption().stream())
   .collect(
       groupingBy(
           flightOption -> flightOption.getOrigin() + flightOption.getDestination(),
           collectingAndThen(
                    mapping(
                      flightOption -> flightOption.getOption(), 
                      toList()
                    ),
                    lists -> lists.get(0)
          )
       )
  );

我想收集airPriceInfo中存在的一些数据,并将其设置在TemporalObject中,同时通过collect操作返回分组数据。最后返回一个TemporalObject列表而不是Map。

class TemporalObject {
    String platingCarrier;
    Map<String, List<Option>> segmentOptions;
}

这可以用流做吗? 谢谢, 乔治

0 个答案:

没有答案