2026-07-19 15:32:13
在Java中,Collectors.toMap 是将 Stream 转换为 Map 的核心工具,其灵活性和功能性使其成为数据转换的常用选择。以下是详细的使用方法和注意事项:
1. 基本用法通过 keyMapper 和 valueMapper 提取键值对,生成默认的 HashMap:
List<User> users = Arrays.asList( new User(1L, "Alice"), new User(2L, "Bob"), new User(3L, "Charlie"));Map<Long, String> idToNameMap = users.stream() .collect(Collectors.toMap(User::getId, User::getName));// 结果:{1=Alice, 2=Bob, 3=Charlie}2. 处理键冲突当流中存在重复键时,默认抛出 IllegalStateException。可通过合并函数(第三个参数)解决:
Map<String, Integer> itemCountMap = items.stream() .collect(Collectors.toMap( Item::getKey, Item::getValue, (existing, replacement) -> existing + replacement // 保留旧值或累加 ));应用场景:统计分类数量、合并重复键的值。
3. 指定Map实现类型默认返回 HashMap,但可通过第四个参数指定其他实现(如 TreeMap):
Map<Long, String> sortedMap = users.stream() .collect(Collectors.toMap( User::getId, User::getName, (e1, e2) -> e1, // 忽略冲突(或自定义合并逻辑) TreeMap::new // 返回按键排序的TreeMap ));4. 空值与异常防护过滤 null 值:.collect(Collectors.toMap( user -> user.getId(), user -> user.getName() != null ? user.getName() : "Unknown"));
或使用允许 null 的 Map 实现(如 HashMap)。
toMap 有三个重载版本:
通过合理使用 Collectors.toMap,可以高效实现 Stream 到 Map 的转换,同时需注意键冲突、空值处理及 Map 类型的选择。