当前位置: 首页 > news >正文

三种主要的重载方法

)两个参数的重载方法(最简单的形式)

public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) { return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new); }

2)三个参数的重载方法(包含冲突处理)

public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction) { return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); }

3)四个参数的重载方法(指定Map实现)

public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) { BiConsumer<M, T> accumulator = (map, element) -> map.merge(keyMapper.apply(element), valueMapper.apply(element), mergeFunction); return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID); }

接下来,我们结合使用示例详细讲解。

2. 使用示例

2.1 将对象的某些属性转换为Map

假设有一个城市列表,需要将其转换为Map,其中Key为城市ID、Value为城市名称,转换方法如下所示:

@Getter @Setter public class City { private Integer cityId; private String cityName; public City(Integer cityId, String cityName) { this.cityId = cityId; this.cityName = cityName; } }
List<City> cityList = Arrays.asList( new City(1, "北京"), new City(2, "上海"), new City(3, "广州"), new City(4, "深圳") ); Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName)); System.out.println(cityMap);

输出结果:

2.2 将对象列表转换为Map(ID -> 对象)

仍然使用上面的城市列表,需要将其转换为Map,其中Key为城市ID、Value为城市对象,转换方法如下所示:

List<City> cityList = Arrays.asList( new City(1, "北京"), new City(2, "上海"), new City(3, "广州"), new City(4, "深圳") ); Map<Integer, City> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, city -> city)); City city = cityMap.get(1); System.out.println("城市ID: " + city.getCityId()); System.out.println("城市名称: " + city.getCityName());

输出结果如下所示:

城市ID: 1
城市名称: 北京

上面的写法等价于:

Map<Integer, City> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, Function.identity()));

因为Function.identity()内部实现是下面这样的:

static <T> Function<T, T> identity() { return t -> t; }

2.3 键冲突处理

假设上面的城市列表中有一个ID重复的城市:

List<City> cityList = Arrays.asList( new City(1, "北京"), new City(2, "上海"), new City(3, "广州"), new City(4, "深圳"), new City(4, "天津") ); Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName)); System.out.println("城市ID: 4, 城市名称: " + cityMap.get(4));

此时运行代码,会抛出java.lang.IllegalStateException异常,如下图所示:

有3种常见的键冲突处理方式,分别是保留旧值、使用新值和合并值,接下来一一讲解。

1)方式一:保留旧值

Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (oldValue, newValue) -> oldValue));

输出结果:

城市ID: 4, 城市名称: 深圳

2)方式二:使用新值

Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (oldValue, newValue) -> newValue));

输出结果:

城市ID: 4, 城市名称: 天津

3)方式三:合并值

Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (oldValue, newValue) -> oldValue + ", " + newValue));

输出结果:

城市ID: 4, 城市名称: 深圳, 天津

2.4 数据分组聚合

假设有一个销售记录列表,需要将其转换为Map,其中Key为销售员、Value为该销售员的总销售额,转换方法如下所示:

@Getter @Setter public class SalesRecord { private String salesPerson; private BigDecimal amount; public SalesRecord(String salesPerson, BigDecimal amount) { this.salesPerson = salesPerson; this.amount = amount; } }
List<SalesRecord> salesRecordList = Arrays.asList( new SalesRecord("张三", new BigDecimal("1000")), new SalesRecord("李四", new BigDecimal("2000")), new SalesRecord("张三", new BigDecimal("980")) ); Map<String, BigDecimal> salesRecordMap = salesRecordList.stream() .collect(Collectors.toMap(SalesRecord::getSalesPerson, SalesRecord::getAmount, BigDecimal::add)); System.out.println(salesRecordMap);

输出结果:

上面的例子是销售额累加,也可以只取最小值:

Map<String, BigDecimal> salesRecordMap = salesRecordList.stream() .collect(Collectors.toMap(SalesRecord::getSalesPerson, SalesRecord::getAmount, BigDecimal::min));

此时的输出结果:

或者只取最大值:

Map<String, BigDecimal> salesRecordMap = salesRecordList.stream() .collect(Collectors.toMap(SalesRecord::getSalesPerson, SalesRecord::getAmount, BigDecimal::max));

此时的输出结果:

2.5 指定Map实现

默认情况下,Collectors.toMap是将结果收集到HashMap中,如果有需要,我们也可以指定成TreeMap或者LinkedHashMap。

如果想要保持插入顺序,可以指定使用LinkedHashMap:

List<City> cityList = Arrays.asList( new City(2, "上海"), new City(1, "北京"), new City(4, "深圳"), new City(3, "广州") ); Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (existing, replacement) -> existing, LinkedHashMap::new)); System.out.println(cityMap);

输出结果:

如果想要按键排序,可以指定使用TreeMap:

List<City> cityList = Arrays.asList( new City(2, "上海"), new City(1, "北京"), new City(4, "深圳"), new City(3, "广州") ); Map<Integer, String> cityMap = cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (existing, replacement) -> existing, TreeMap::new)); System.out.println(cityMap);

输出结果:

http://www.cnnetsun.cn/news/3086773.html

相关文章:

  • PIC18F46K22与25CSM04 EEPROM高速数据存储方案
  • 基于KMR221与PIC18F4458的高精度电压监测系统设计
  • 【JAVA毕设源码分享】基于springboot毕业生就业系统的设计与实现(程序+文档+代码讲解+一条龙定制)
  • ITK-SNAP医学图像分割工具完整教程:从入门到精通
  • nginx 基础核心
  • 大模型落地避坑手册(Claude与ChatGPT实战差异全拆解):从API稳定性、token计费陷阱到隐私审计红线
  • 如何用Unlock-Music免费解锁加密音乐:打破平台限制的终极指南
  • Android模拟器HTTPS流量解密全流程:从Mitmproxy配置到证书锁定突破
  • 高性能MCU驱动LED矩阵:IS31FL3731与PIC32MZ实战
  • Platinum-MD:3个步骤让你的老式MiniDisc播放器重获新生,体验无损音乐传输的乐趣
  • 如何快速提升网盘下载效率:终极免费解决方案指南
  • Minecraft 1.21终极中文体验:Masa模组全家桶汉化完整指南
  • 免费音频编辑终极指南:Audacity如何帮你轻松处理声音?
  • 测试用来测试
  • QuickVina 2:突破性分子对接加速技术的完整指南
  • VLC鼠标点击暂停插件:重新定义视频播放控制体验
  • 遗传算法实战:N皇后问题的Python高效实现与调优
  • 电商运营自动化实战:多平台数据采集与订单同步完整方案
  • 深度学习模型推理框架_SNPT 对比 TRT
  • 为什么92%的企业选错大模型?——基于217家客户POC结果的AI选型失效根因分析及迁移路径图
  • 3分钟免费安装:Windows鼠标指针蔚蓝档案主题终极指南
  • 3个步骤让你的WiFi信号死角无处遁形:家庭网络可视化优化指南
  • cri-docker 内存泄露(memory leak)问题
  • 遗传算法实战:N皇后问题的Python可调试实现
  • test01
  • Zotero插件市场:3步彻底告别繁琐的手动插件安装
  • AI代理桌面应用网络安全配置实战:从威胁分析到纵深防御
  • MounRiver Studio自定义主题
  • 鸣潮自动化工具终极指南:免费实现后台智能战斗与资源收集
  • 2026年热门阅读软件实测,一篇讲明白