- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于 Collectors.groupingBy()
返回 Map<K,List<T>>
是否暗示 List<T>
是为了评估流吗?
我没有看到对列表排序的明确描述,而并发版本明确说明没有排序。如果它不是以某种方式订购的,我希望它是一个集合,但我看不出它可能是什么其他订购,除了收到的订单。
我希望它能保证每个列表中的最后一个值是该组收到的最后一个值。
最佳答案
groupingBy()
的文档说:
Implementation Requirements:
This produces a result similar to:
groupingBy(classifier, toList());
toList()
的文档说:
Returns:
a
Collector
which collects all the input elements into aList
, in encounter order
因此,要回答您的问题,只要您的流具有已定义的 encounter order ,您一定会得到有序列表。
编辑:正如@Holger 指出的那样,groupingBy()
还必须遵守遇到顺序以保留toList()
的排序约束。此说明中强烈暗示了它确实存在的事实:
Implementation Note:
...If preservation of the order in which elements are presented to the downstream collector is not required, using
groupingByConcurrent(Function, Collector)
may offer better parallel performance.
关于Java Collectors.groupingBy()---List 是有序的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39172981/
有没有一种方法可以简化它并直接将从 groupingBy 获得的映射转换为具有键和值作为属性的元素列表?而不是有 2 倍的转换流。 我在这里做的是获取 RiskItems,然后将它们映射到 DTO,之
我面临一个棘手的情况,我必须在具有嵌套列表的对象上使用 groupingBy。我用 map()、flatmap()、toMap() 尝试了一些东西,但无法想出一个解决方案,只是在兜圈子。非常感谢来自流
我想知道流(或收集器)中是否已经实现了将列表排序为值的功能。例如。以下代码都生成按性别分组的人员列表,并按年龄排序。第一个解决方案有一些开销排序(并且看起来有点邋遢)。第二种解决方案需要对每个人进行两
这个问题在这里已经有了答案: Java 8 is not maintaining the order while grouping (2 个答案) Stream doesn't preserve t
我有一个 List>想要一个 Map>>这将映射每个 Integer所有 sets包含 Integer . 这里明显的候选者是使用 Collectors.groupingBy但它不起作用,因为 gro
我想知道,流(或收集器)中是否已经实现了将列表排序为值的功能。例如。以下代码均生成按年龄排序的按性别分组的人员列表。第一个解决方案有一些开销排序(看起来有点邋遢)。第二种解决方案需要对每个人进行两次检
我正在尝试做的一个简化示例: 假设我有一个字符串列表,需要根据是否包含特定子字符串的条件将其分为 4 组。如果一个字符串包含 Foo 它应该属于 FOO 组,如果它包含 Bar 它应该属于 BAR 组
我想通过基于 LocalDateTime 的集合进行分组,但我只想获取小时,而不是分钟、秒... .collect(Collectors.groupingBy(cp -> getUpdateLocal
我有一个Employee类: public class Employee { public enum Sex { MALE, FEMALE } private
考虑this SO answer 我有几个问题: 它是如何运作的?是groupingBy()检查返回值是否来自classifier是 List并比较每一项?对我来说,这听起来不像是典型的 Java 函
我有一个College具有嵌套静态类的类 Dept 学院 class College { private String collegeName; private Dept dept; public D
编译器在这里给了我一个非静态方法错误,我已经知道这并不意味着它一定是问题,但我真的找不到其他任何东西,特别是因为我在不同的类中有相同的方法对于通行证来说,一切正常。 public Map> setup
我有一个Employee类: public class Employee { public enum Sex { MALE, FEMALE } private
考虑this SO answer 我有几个问题: 它是如何运作的?是groupingBy()检查返回值是否来自classifier是 List并比较每一项?对我来说,这听起来不像是典型的 Java 函
我有一个类,假设 CheeseMojo 具有以下各种字段: public class CheeseMojo { private String recipies = "Recipies";
tl/dr:Kotlin 如何使用 groupingBy 和聚合来获得(键,数字)对的序列以求和到计数映射? 我有 30gb 的 csv 文件,可以轻松阅读和解析。 File("data").walk
我有一个 Employee 的列表 public class Employee { private String name; private Integer age; private Do
我有一个类叫做 Food : public class Food { private Collection foodToFoodGroups; //getters and setter
假设我有一个国家/地区对象列表,其中包含该国家/地区使用的语言列表,如下所示: class Country { List languages; } 我想创建以下格式的 map : Map> ,
给定一个 TimeEntry 对象列表,我想将它们分组为 Day 和 Hour 对象。 其中 Day 有 Hour 对象的列表,而 Hour 有其代表的小时的 TimeEntry 对象的列表。 Day
我是一名优秀的程序员,十分优秀!