list stream 最大和最小值_Java 8 – Stream – 按值分组并查找该对象的最小值和最大值...

如果您对每组只有一辆车感兴趣,可以使用,例如

Map mostExpensives = carsDetails.stream()

.collect(Collectors.toMap(Car::getMake, Function.identity(),

BinaryOperator.maxBy(Comparator.comparing(Car::getPrice))));

mostExpensives.forEach((make,car) -> System.out.println(make+" "+car));

但既然你想要最便宜和最便宜的,你需要这样的东西:

Map> mostExpensivesAndCheapest = carsDetails.stream()

.collect(Collectors.toMap(Car::getMake, car -> Arrays.asList(car, car),

(l1,l2) -> Arrays.asList(

(l1.get(0).getPrice()>l2.get(0).getPrice()? l2: l1).get(0),

(l1.get(1).getPrice()

mostExpensivesAndCheapest.forEach((make,cars) -> System.out.println(make

+" cheapest: "+cars.get(0)+" most expensive: "+cars.get(1)));

由于没有与DoubleSummaryStatistics等效的通用统计对象,因此该解决方案带来一些不便.如果这种情况不止一次发生,那么值得用这样的类填补空白:

/**

* Like {@code DoubleSummaryStatistics}, {@code IntSummaryStatistics}, and

* {@code LongSummaryStatistics}, but for an arbitrary type {@code T}.

*/

public class SummaryStatistics implements Consumer {

/**

* Collect to a {@code SummaryStatistics} for natural order.

*/

public static > Collector>

statistics() {

return statistics(Comparator.naturalOrder());

}

/**

* Collect to a {@code SummaryStatistics} using the specified comparator.

*/

public static Collector>

statistics(Comparator comparator) {

Objects.requireNonNull(comparator);

return Collector.of(() -> new SummaryStatistics<>(comparator),

SummaryStatistics::accept, SummaryStatistics::merge);

}

private final Comparator c;

private T min, max;

private long count;

public SummaryStatistics(Comparator comparator) {

c = Objects.requireNonNull(comparator);

}

public void accept(T t) {

if(count == 0) {

count = 1;

min = t;

max = t;

}

else {

if(c.compare(min, t) > 0) min = t;

if(c.compare(max, t) < 0) max = t;

count++;

}

}

public SummaryStatistics merge(SummaryStatistics s) {

if(s.count > 0) {

if(count == 0) {

count = s.count;

min = s.min;

max = s.max;

}

else {

if(c.compare(min, s.min) > 0) min = s.min;

if(c.compare(max, s.max) < 0) max = s.max;

count += s.count;

}

}

return this;

}

public long getCount() {

return count;

}

public T getMin() {

return min;

}

public T getMax() {

return max;

}

@Override

public String toString() {

return count == 0? "empty": (count+" elements between "+min+" and "+max);

}

}

将此添加到您的代码库后,您可以使用它

Map> mostExpensives = carsDetails.stream()

.collect(Collectors.groupingBy(Car::getMake,

SummaryStatistics.statistics(Comparator.comparing(Car::getPrice))));

mostExpensives.forEach((make,cars) -> System.out.println(make+": "+cars));

如果getPrice返回double,则使用Comparator.comparingDouble(Car :: getPrice)而不是Comparator.comparing(Car :: getPrice)可能更有效.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值