java - GroupBy 之后的 Java 排序
问题描述
如何将这些排序为 SortedMap 而不仅仅是 Map?
class Person {
String name;
double weight;
public Person(String name, double weight) {
this.name = name;
this.weight = weight;
}
public double getWeight() {
return this.weight;
}
}
List<Person> people = Arrays.asList(
new Person("Alex", 150.0),
new Person("Courtney", 120.0),
new Person("Billy", 180.0)
);
Map<Double, List<Person>> grouped = people.stream()
.collect(Collectors.groupingBy(Person::getWeight));
解决方案
您可以SortedMap
像这样提供结果:
final Map<Double, List<Person>> grouped = new TreeMap<>(
people.stream().collect(Collectors.groupingBy(Person::getWeight)));
推荐阅读
- amazon-web-services - 自定义授权者的 AWS API 网关 403 错误
- php - 帖子未删除,laravel 7中的服务器错误500
- python - 如何消除 Python 尝试的延迟:除了:
- apache - 在 ubuntu 上部署 cakephp 应用程序时出错
- ruby-on-rails - 如何正确地将一个散列合并到另一个散列中,替换第一个键。回报率
- flutter - 在 Flutter SQFLite 数据库中监听实时变化
- python - 如何为 Spyder 安装 python 包
- java - SonarQube 不索引/查找 JAVA 文件
- c# - 通过 SDK 或 Interop 从 byte[] 打开 .doc 或 .xls
- javascript - VueJS中更新数据的机制?