-1

我有一个List以薪水为特征的员工。为什么这段代码不起作用?

String joined = employees.stream().collect(
    Collectors.summingInt(Employee::getSalary),
    Collectors.maxBy(Comparator.comparing(Employee::getSalary)),
    Collectors.minBy(Comparator.comparing(Employee::getSalary)),
    Collectors.averagingLong((Employee e) ->e.getSalary() * 2),
    Collectors.counting(),
    Collectors.joining(", "));

我正在使用一套收集器。

4

2 回答 2

4

请注意,目前您试图获得的不是最高/最低薪水,而是拥有这样薪水的员工。如果您真的想拥有最高/最低工资本身(数字),那么可以使用以下方法一次计算这些特征Collectors.summarizingInt()

IntSummaryStatistics stats = employees.stream()
    .collect(Collectors.summarizingInt(Employee::getSalary));

如果要将它们加入字符串,可以使用:

String statsString = Stream.of(stats.getSum(), stats.getMax(), stats.getMin(), 
                               stats.getAverage()*2, stats.getCount())
                           .map(Object::toString)
                           .collect(Collectors.joining(", "));

如果您真的想获得最高/最低工资的员工,这里IntSummaryStatistics不会帮助您。但是,您可以改为创建收集器流:

String result = Stream.<Collector<Employee,?,?>>of(
            Collectors.summingInt(Employee::getSalary),
            Collectors.maxBy(Comparator.comparing(Employee::getSalary)),
            Collectors.minBy(Comparator.comparing(Employee::getSalary)),
            Collectors.averagingLong((Employee e) ->e.getSalary() * 2),
            Collectors.counting())
        .map(collector -> employees.stream().collect(collector))
        .map(Object::toString)
        .collect(Collectors.joining(", "));

请注意,通过这种方式,您将获得类似的输出(取决于Employee.toString()实现:

1121, Optional[Employee [salary=1000]], Optional[Employee [salary=1]], 560.5, 4

不要忘记maxBy/ minByreturn Optional


如果您对第一个解决方案不满意并且由于某种原因不想多次迭代输入,您可以使用如下方法创建组合收集器:

/**
 * Returns a collector which joins the results of supplied collectors
 * into the single string using the supplied delimiter.
 */
@SafeVarargs
public static <T> Collector<T, ?, String> joining(CharSequence delimiter, 
        Collector<T, ?, ?>... collectors) {
    @SuppressWarnings("unchecked")
    Collector<T, Object, Object>[] cs = (Collector<T, Object, Object>[]) collectors;
    return Collector.<T, Object[], String>of(
        () -> Stream.of(cs).map(c -> c.supplier().get()).toArray(), 
        (acc, t) -> IntStream.range(0, acc.length)
            .forEach(idx -> cs[idx].accumulator().accept(acc[idx], t)), 
        (acc1, acc2) -> IntStream.range(0, acc1.length)
            .mapToObj(idx -> cs[idx].combiner().apply(acc1[idx], acc2[idx]))
            .toArray(), 
        acc -> IntStream.range(0, acc.length)
            .mapToObj(idx -> cs[idx].finisher().apply(acc[idx]).toString())
            .collect(Collectors.joining(delimiter)));
}

有这样的方法你可以写

String stats = employees.stream().collect(joining(", ",
        Collectors.summingInt(Employee::getSalary),
        Collectors.maxBy(Comparator.comparing(Employee::getSalary)),
        Collectors.minBy(Comparator.comparing(Employee::getSalary)),
        Collectors.averagingLong((Employee e) ->e.getSalary() * 2),
        Collectors.counting()));
于 2015-12-15T04:28:33.860 回答
1

我终于找到了解决方案.. 感谢您的尝试

String s = employees.stream().mapToDouble(a>a.getSalary()).summaryStatistics().toString();

这是输出:

 DoubleSummaryStatistics{count=21, sum=17200,000000, min=100,000000,
 average=819,047619, max=2100,000000}
于 2015-12-15T15:36:47.960 回答