Java 8流 - 如何使用整数键获得相当于分区的()?
在下面所示的代码中,p
是类型peedicate< string>
的谓词。
Map<Boolean, List<String>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(p));
是否可以将boolean
密钥转换为int
在分区逻辑中键入而不是具有另一个流?
看起来可以通过分组
来完成。
Map<String, List<String>> targetTableColumnListMap = nqColumnMapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(
e -> e.getKey().toUpperCase(),
Collectors.mapping(Map.Entry::getValue, Collectors.toList())
));
In the code shown below, p
is a predicate of type Predicate<String>
.
Map<Boolean, List<String>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(p));
Is it possible to convert the boolean
keys to the int
type inside the partitioning logic rather than having another stream?
Looks like it could be done with grouping
.
Map<String, List<String>> targetTableColumnListMap = nqColumnMapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(
e -> e.getKey().toUpperCase(),
Collectors.mapping(Map.Entry::getValue, Collectors.toList())
));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
,因此您需要将列表中的值与
0
和1
(例如)相关联,以将数据存储在数据库中 )基于给定谓词。为此,代替
partitioningby()
您可以使用collectorgroupingby()
与 。So you need to associate the values in a list with
0
and1
(for instance, in order to store the data into the database) based on the given predicate.For that, instead of
partitioningBy()
you can use collectorgroupingBy()
in conjunction with the predicate.您可以将终端操作
collect(collectors.tomap())
通过您的谓词进行分组,而不是返回其boolean
值作为密钥,您可以使用三元操作员来返回1或0。,您可以映射到该键
list
构建的String
您测试了predication
要处理碰撞案例,您可以将与第二个密钥关联的list
添加到list
对应于第一个密钥的情况下。这是代码的片段:
这里还有一个测试代码的链接:
https://ideone.com/kd8xri < /a>
You could use the terminal operation
collect(Collectors.toMap())
to group by your predicate and instead of returning itsboolean
value as the key you could use a ternary operator to return either 1 or 0.Then, you could map to that key a
List
built on theString
you tested thePredicate
with, and to handle the colliding cases you could add theList
associated to the second key into theList
corresponding to the first key.Here's a snippet of the code:
Here, there's also a link to test the code:
https://ideone.com/Kd8xrI
假设下面的类 -
//现在要根据性别进行分组或获取员工的计数,我们也可以使用以下代码
//也可以使用过滤器
Assuming that below class -
// Now to group or get the count of, Employees based on Gender, we can use below codes
// Filters can be used as well