ストリームマップフィルタおよびJava 8で元のオブジェクトにマップバックする方法はありますか?

AshwinK:

ストリームにどのような方法は、リストがある - >マップ - >フィルタ - >リストの元のオブジェクト型に戻ってマッピングしますか?

私たちは以下のようにしますforeachを使用してそれをやっている場合は解決策があります:

List<Query> updatedQueries = getUpdatedQueries();

List<Query> finalQueries = new ArrayList<>();
updatedQueries.forEach(query -> {

    Period period = getPeriodRequest(query);
    boolean isValidPeriod = periodService.validatePeriodicity(period);
    if(isValidPeriod &&  isMandatory(period)){
        finalQueries.add(query);
    }

});

しかし、次の方法を使用してそれを行うにはどのような方法はありますか?

List<Query> updatedQueries = getUpdatedQueries();

List<Query> finalQueries = updatedQueries
        .stream()
        .map(this::getPeriodRequest) //returns the object of type Period
        .filter(period->periodService.validatePeriodicity(period))
        .filter(this::isMandatory)
        //is any way we can map back to Query object (without any object translation  function)
        .collect(Collectors.toList());
ハディJ:

これを試してみてください

List<Query> finalQueries = updatedQueries
    .stream().filter(query->{
        Period period = getPeriodRequest(query);
        return periodService.validatePeriodicity(period )&& isMandatory(period))
    })
    .collect(Collectors.toList());

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=182779&siteId=1