DerBenniAusA:
どのようにして渡すことができます.min()
か.max()
、このようなコードでメソッドのパラメータとして式を:
与えられたコード:
private LocalDate getMaxDate() {
LocalDate maxdate = dates.stream()
.max( Comparator.comparing( LocalDate::toEpochDay ) )
.get();
}
private LocalDate getMinDate() {
LocalDate maxdate = dates.stream()
.min( Comparator.comparing( LocalDate::toEpochDay ) )
.get();
}
私が持っていることを期待していることコード:
private LocalDate getDate(SomeType _EXPR_){
LocalDate maxdate = dates.stream()
._EXPR_( Comparator.comparing( LocalDate::toEpochDay ) )
.get();
}
ヒント:_EXPR_
しなければならない.min()
、時には、.max()
アンドリューTobilko:
方法があります
private LocalDate getDate(Function<Comparator<LocalDate>, BinaryOperator<LocalDate>> f) {
return dates.stream()
.reduce(f.apply(Comparator.comparing(LocalDate::toEpochDay)))
.get();
}
、使用それを呼び出すには
getDate(BinaryOperator::maxBy);
getDate(BinaryOperator::minBy);
用心NoSuchElementException
たOptional#get
スローすることがあります。