使用Stream和java8以一个字符串返回工资的最小值,最大值,平均值,总和,计数

时间:2015-12-15 01:36:26

标签: java java-8 java-stream collectors

我有一个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(", "));

我正在使用一套收藏家。

2 个答案:

答案 0 :(得分:3)

请注意,目前您正在尝试获得最高/最低工资,但员工有这样的工资。如果您确实希望获得最大/最小工资(数字),那么可以使用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 / minBy返回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()));

答案 1 :(得分:0)

我终于找到了解决方案..感谢你们的努力

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}