我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎和作为可执行 JAR 文件的包我想创建一个基于 POJO 的自定义 SummaryStatistics,该 POJO 具有 2 个字段,价格和更新日期。此统计信息应该获得最低/最高价格和最低/最高价格的日期。
public class MenuPriceSummaryStatistics implements Consumer<MenuPrice> {
private long count;
private double sum;
private double sumCompensation; // Low order bits of sum
private double simpleSum; // Used to compute right sum for non-finite inputs
private double min = Double.POSITIVE_INFINITY;
private double max = Double.NEGATIVE_INFINITY;
private Date maxDate;
private Date minDate;
public MenuPriceSummaryStatistics() {
}
/**
* Combines the state of another {@code DoubleSummaryStatistics} into this one.
*
* @param other
* another {@code DoubleSummaryStatistics}
* @throws NullPointerException
* if {@code other} is null
*/
public void combine(MenuPriceSummaryStatistics other) {
count += other.count;
simpleSum += other.simpleSum;
sumWithCompensation(other.sum);
sumWithCompensation(other.sumCompensation);
min = Math.min(min, other.min);
max = Math.max(max, other.max);
if (min==other.min) {
minDate=other.minDate;
}
if (max==other.max) {
maxDate=other.maxDate;
}
}
@Override
public void accept(MenuPrice cp) {
++count;
simpleSum += cp.getPrice();
sumWithCompensation(cp.getPrice());
min = Math.min(min, cp.getPrice());
max = Math.max(max, cp.getPrice());
if (min==cp.getPrice()) {
minDate=cp.getUpdateDate();
}
if (max==cp.getPrice()) {
maxDate=cp.getUpdateDate();
}
}
private void sumWithCompensation(double value) {
double tmp = value - sumCompensation;
double velvel = sum + tmp; // Little wolf of rounding error
sumCompensation = (velvel - sum) - tmp;
sum = velvel;
}
public final long getCount() {
return count;
}
public final double getSum() {
// Better error bounds to add both terms as the final sum
double tmp = sum + sumCompensation;
if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
// If the compensated sum is spuriously NaN from
// accumulating one or more same-signed infinite values,
// return the correctly-signed infinity stored in
// simpleSum.
return simpleSum;
else
return tmp;
}
public final double getMin() {
return min;
}
public final double getMax() {
return max;
}
public Date getMaxDate() {
return maxDate;
}
public Date getMinDate() {
return minDate;
}
public final double getAverage() {
return getCount() > 0 ? getSum() / getCount() : 0.0d;
}
@Override
public String toString() {
return "MenuPriceSummaryStatistics [count=" + count + ", sum=" + sum + ", sumCompensation="
+ sumCompensation + ", simpleSum=" + simpleSum + ", min=" + min + ", max=" + max + ", maxDate="
+ maxDate + ", minDate=" + minDate + "]";
}
}
但是当我尝试使用它时
MenuPriceSummaryStatistics statistics =
(menuPrices
.stream()
.map(cp -> cp))
.summaryStatistics();
我得到一个错误,因为我应该定义.summaryStatistics();
我试图定义类的方法
public interface MenuPriceStream extends BaseStream<MenuPrice, MenuPriceStream> {
..
}
但后来我变得复杂了
使用建议的解决方案:
public static Collector<MenuPrice, ?, MenuPriceSummaryStatistics> menuPriceSummary() {
return Collector.of(
MenuPriceSummaryStatistics::new,
MenuPriceSummaryStatistics::accept,
(a, b) -> {a.combine(b); return a;},
Collector.Characteristics.IDENTITY_FINISH
);
}
我有这个编译错误:
Multiple markers at this line
- Syntax error, insert "[ ]" to complete Dimension
- Syntax error, insert ";" to complete LocalVariableDeclarationStatement
- Syntax error on token "menuPriceSummary", AnnotationName expected after this
token