saj*_*jib 4 java java-8 spring-data-jpa spring-boot
如何使用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)));
}
Run Code Online (Sandbox Code Playgroud)
使用Stream:
IntStream.range(1,searchCriteria.getConditions().size())
.mapToObj(index-> getSpec(searchCriteria.getConditions().get(index)))
.collect();//how to merge with calling and
Run Code Online (Sandbox Code Playgroud)
相关类&&方法:
@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){
....
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确:
IntStream.range(0, searchCriteria.getConditions().size())
.mapToObj(index-> getSpec(searchCriteria.getConditions().get(index)))
.reduce(Specification::and)
.orElseThrow(SomeException::new) // or any default from Specification...
Run Code Online (Sandbox Code Playgroud)
甚至更好:
searchCriteria.getConditions()
.stream()
.map(this::getSpec)
.reduce(Specification::and)
.orElseThrow(SomeException::new)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
459 次 |
最近记录: |