Java 8 java8基于聚合和的流分组和排序
给java类一些东西Java 8 java8基于聚合和的流分组和排序,java-8,grouping,aggregate,java-stream,Java 8,Grouping,Aggregate,Java Stream,给java类一些东西 class Something { private int parentKey; private String parentName; private int childKey; private int noThings; public Something(int parentKey, String parentName, int childKey, int noThings) { this.parentKey = parentKey
class Something {
private int parentKey;
private String parentName;
private int childKey;
private int noThings;
public Something(int parentKey, String parentName, int childKey,
int noThings) {
this.parentKey = parentKey;
this.parentName = parentName;
this.childKey = childKey;
this.noThings = noThings;
}
public int getParentKey() {
return this.parentKey;
}
public int getNoThings() {
return this.noThings;
}
}
我有一张物品清单
List<Something> somethings = newArrayList(
new Something(425, "Lemon", 44, 23),
new Something(123, "Orange", 125, 66),
new Something(425, "Lemon", 11, 62),
new Something(123, "Orange", 126, 32),
new Something(323, "Lime", 25, 101),
new Something(123, "Orange", 124, 88)
);
但这似乎是一个很大的工作,不是很优雅
如果您有任何意见/想法,我们将不胜感激。您已经通过收集汇总信息完成了主要工作
Map<Integer, Integer> totalNoThings = somethings.stream()
.collect(Collectors.groupingBy(Something::getParentKey,
Collectors.summingInt(Something::getNoThings)));
Map totalNoThings=somethings.stream()
.collect(收集器).groupingBy(某物::getParentKey,
收集者。汇总(某物::getNoThings));
然后,您只需在排序操作中利用这些信息:
List<Something> sorted=somethings.stream().sorted(
Comparator.comparing((Something x)->totalNoThings.get(x.getParentKey()))
.thenComparing(Something::getNoThings).reversed())
.collect(Collectors.toList());
List sorted=somethings.stream().sorted(
Comparator.comparing((Something x)->totalNoThings.get(x.getParentKey())
.thenComparing(Something::getNoThings).reversed())
.collect(Collectors.toList());
实际上需要做一个小小的调整,而不是totalNoThings.get,它是totalNoThings.indexOf
所以最后的解决方案是
List<Integer> totalNoThings
= somethings.stream()
.collect(Collectors.groupingBy(Something::getParentKey,
Collectors.summingInt(Something::getNoThings)))
.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.collect(Collectors.toList());
List<Something> sorted
= somethings.stream().sorted(
Comparator.comparing(
(Something obj)->totalNoThings.indexOf(
obj.getParentKey()))
.thenComparing(Something::getNoThings).reversed())
.collect(Collectors.toList());
List totalNoThings
=something.stream()
.collect(收集器).groupingBy(某物::getParentKey,
Collectors.summingit(Something::getNoThings)))
.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.map(map.Entry::getKey)
.collect(Collectors.toList());
列表排序
=something.stream().sorted(
比较器(
(某物对象)->totalNoThings.indexOf(
obj.getParentKey())
.thenComparing(Something::getNoThings).reversed())
.collect(Collectors.toList());
谢谢!经过一番代码角力后,我正要发布答案!我的解决方案没有你的好,但遵循同样的逻辑。
Map<Integer, Integer> totalNoThings = somethings.stream()
.collect(Collectors.groupingBy(Something::getParentKey,
Collectors.summingInt(Something::getNoThings)));
List<Something> sorted=somethings.stream().sorted(
Comparator.comparing((Something x)->totalNoThings.get(x.getParentKey()))
.thenComparing(Something::getNoThings).reversed())
.collect(Collectors.toList());
List<Integer> totalNoThings
= somethings.stream()
.collect(Collectors.groupingBy(Something::getParentKey,
Collectors.summingInt(Something::getNoThings)))
.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.collect(Collectors.toList());
List<Something> sorted
= somethings.stream().sorted(
Comparator.comparing(
(Something obj)->totalNoThings.indexOf(
obj.getParentKey()))
.thenComparing(Something::getNoThings).reversed())
.collect(Collectors.toList());