java - Modifying the returned Map value type with collectors.groupingBy in Java stream
问题描述
I have below the class definition of Entry
-
public class Entry {
private String key;
private String Value;
// getters and setters
// equals hashcode toString
}
I got List of Entry
objects from database.
I want to group them based on key and values of the result Map should be Set<value>
.
I tried and end up with the below code.
Map<String, Set<Entry>> groupedEntries =
entryList.findAll()
.stream()
.collect(Collectors.groupingBy(ek -> ek.getKey().toLowerCase(), Collectors.toSet()));
The problem with this code is the result type is Map<String, Set<Entry>>
but I want to be Map<String, Set<String>>
.
is it possible to do this in single collect?
解决方案
use the mapping
collector:
.collect(Collectors.groupingBy(ek -> ek.getKey().toLowerCase(),
Collectors.mapping(Entry::getValue, Collector.toSet())));
推荐阅读
- android - 具有动态值的嵌套 for 循环以显示在 Android Studio 中 RecyclerView 上的微调器中
- c# - 如何在 .net 中将日志写入 cloudwatch
- c - 如何将程序发送到后台并停止执行?
- sql - 拆分列值并移动到行
- c# - 如何将 Windows 上的 Redis 连接到远程服务器
- visual-studio-2017 - 在 Visual Studio 2017 中使用 FolderProfile 发布时挂钩“发布后”事件
- django - 在时区支持处于活动状态时收到一个天真的日期时间
- c# - 如何保证线程的执行顺序
- java - 使我的应用程序能够自行获取辅助功能权限
- sql - 在存储过程语法中使用 case-when 语句