java - Java Streams – 如何按值分组并找到每个组的最小值和最大值?
问题描述
对于我的示例,拥有汽车对象并发现基于模型(分组依据)的最小和最大价格值。
List<Car> carsDetails = UserDB.getCarsDetails();
Map<String, DoubleSummaryStatistics> collect4 = carsDetails.stream()
.collect(Collectors.groupingBy(Car::getMake, Collectors.summarizingDouble(Car::getPrice)));
collect4.entrySet().forEach(e->System.out.println(e.getKey()+" "+e.getValue().getMax()+" "+e.getValue().getMin()));
output :
Lexus 94837.79 17569.59
Subaru 96583.25 8498.41
Chevrolet 99892.59 6861.85
但我找不到哪些汽车对象具有最高和最低价格。我怎样才能做到这一点?
解决方案
如果您只对Car
每组一个感兴趣,您可以使用,例如
Map<String, Car> 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<String, List<Car>> 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()<l2.get(1).getPrice()? l2: l1).get(1))));
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<T> implements Consumer<T> {
/**
* Collect to a {@code SummaryStatistics} for natural order.
*/
public static <T extends Comparable<? super T>> Collector<T,?,SummaryStatistics<T>>
statistics() {
return statistics(Comparator.<T>naturalOrder());
}
/**
* Collect to a {@code SummaryStatistics} using the specified comparator.
*/
public static <T> Collector<T,?,SummaryStatistics<T>>
statistics(Comparator<T> comparator) {
Objects.requireNonNull(comparator);
return Collector.of(() -> new SummaryStatistics<>(comparator),
SummaryStatistics::accept, SummaryStatistics::merge);
}
private final Comparator<T> c;
private T min, max;
private long count;
public SummaryStatistics(Comparator<T> 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<T> merge(SummaryStatistics<T> 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<String, SummaryStatistics<Car>> 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)
.
推荐阅读
- python - 未从 Python 执行的 MS SQL 存储过程
- node.js - 为什么我的 AJAX 请求没有通过?
- java - 为什么 MicroMeter Timer 返回零?
- excel - 当我想使用应用程序属性时,我在 Excel 上遇到问题
- .htaccess - 为什么这个 htaccess RewriteRule 不“坚持”?
- ios - TestFlight 应用内购买返回非常旧的收据,其中 productID 不再存在
- javascript - 沟通php和js
- excel - 将特定术语从 Microsoft Word 提取到 Excel
- php - 将错误消息放入 URL [PHP] 的目的是什么?
- recursion - int list 的累计和