仕様一覧はjava8ストリームを使用して組み合わせます

Golam Mazidのsajib:

このコードJava8ストリームを使用する方法:

Specification<T> specification = specifications.getSpec(searchCriteria.getConditions().get(0));
        for(int i = 1; i < searchCriteria.getConditions().size(); i++) {
            specification = specification.and(getSpec(searchCriteria.getConditions().get(i)));
    }

ストリームを使用します:

  IntStream.range(1,searchCriteria.getConditions().size())
                    .mapToObj(index-> getSpec(searchCriteria.getConditions().get(index)))
                    .collect();//how to merge with calling and

関連クラス&&方法:

@Getter
@Setter
public class SearchCriteria implements Serializable{

    private static final long serialVersionUID = 1L;

    private List<Condition> conditions;
    private Integer limit;
    private Integer offset;

    @Getter
    @Setter
    public class Condition{
        private String key;
        private EConstant.OPERATION operation;
        private String value;
    }
}
public Specification<T> getSpec(SearchCriteria.Condition condition){
....
}
ユージン:

私が正しく理解している場合:

 IntStream.range(0, searchCriteria.getConditions().size())
         .mapToObj(index-> getSpec(searchCriteria.getConditions().get(index)))
         .reduce(Specification::and)
         .orElseThrow(SomeException::new) // or any default from Specification...

あるいはさらに良いです:

 searchCriteria.getConditions()
               .stream()
               .map(this::getSpec)
               .reduce(Specification::and)
               .orElseThrow(SomeException::new)

おすすめ

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