三种主要的重载方法

发布时间:2026/7/1 15:23:55
三种主要的重载方法 )两个参数的重载方法最简单的形式public static T, K, U CollectorT, ?, MapK,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 CollectorT, ?, MapK,U toMap(Function? super T, ? extends K keyMapper, Function? super T, ? extends U valueMapper, BinaryOperatorU mergeFunction) { return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); }3)四个参数的重载方法指定Map实现public static T, K, U, M extends MapK, U CollectorT, ?, M toMap(Function? super T, ? extends K keyMapper, Function? super T, ? extends U valueMapper, BinaryOperatorU mergeFunction, SupplierM mapSupplier) { BiConsumerM, 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; } }ListCity cityList Arrays.asList( new City(1, 北京), new City(2, 上海), new City(3, 广州), new City(4, 深圳) ); MapInteger, String cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName)); System.out.println(cityMap);输出结果2.2 将对象列表转换为MapID - 对象仍然使用上面的城市列表需要将其转换为Map其中Key为城市ID、Value为城市对象转换方法如下所示ListCity cityList Arrays.asList( new City(1, 北京), new City(2, 上海), new City(3, 广州), new City(4, 深圳) ); MapInteger, 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城市名称: 北京上面的写法等价于MapInteger, City cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, Function.identity()));因为Function.identity()内部实现是下面这样的static T FunctionT, T identity() { return t - t; }2.3 键冲突处理假设上面的城市列表中有一个ID重复的城市ListCity cityList Arrays.asList( new City(1, 北京), new City(2, 上海), new City(3, 广州), new City(4, 深圳), new City(4, 天津) ); MapInteger, String cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName)); System.out.println(城市ID: 4, 城市名称: cityMap.get(4));此时运行代码会抛出java.lang.IllegalStateException异常如下图所示有3种常见的键冲突处理方式分别是保留旧值、使用新值和合并值接下来一一讲解。1)方式一保留旧值MapInteger, String cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (oldValue, newValue) - oldValue));输出结果城市ID: 4, 城市名称: 深圳2)方式二使用新值MapInteger, String cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (oldValue, newValue) - newValue));输出结果城市ID: 4, 城市名称: 天津3)方式三合并值MapInteger, 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; } }ListSalesRecord salesRecordList Arrays.asList( new SalesRecord(张三, new BigDecimal(1000)), new SalesRecord(李四, new BigDecimal(2000)), new SalesRecord(张三, new BigDecimal(980)) ); MapString, BigDecimal salesRecordMap salesRecordList.stream() .collect(Collectors.toMap(SalesRecord::getSalesPerson, SalesRecord::getAmount, BigDecimal::add)); System.out.println(salesRecordMap);输出结果上面的例子是销售额累加也可以只取最小值MapString, BigDecimal salesRecordMap salesRecordList.stream() .collect(Collectors.toMap(SalesRecord::getSalesPerson, SalesRecord::getAmount, BigDecimal::min));此时的输出结果或者只取最大值MapString, BigDecimal salesRecordMap salesRecordList.stream() .collect(Collectors.toMap(SalesRecord::getSalesPerson, SalesRecord::getAmount, BigDecimal::max));此时的输出结果2.5 指定Map实现默认情况下Collectors.toMap是将结果收集到HashMap中如果有需要我们也可以指定成TreeMap或者LinkedHashMap。如果想要保持插入顺序可以指定使用LinkedHashMapListCity cityList Arrays.asList( new City(2, 上海), new City(1, 北京), new City(4, 深圳), new City(3, 广州) ); MapInteger, String cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (existing, replacement) - existing, LinkedHashMap::new)); System.out.println(cityMap);输出结果如果想要按键排序可以指定使用TreeMapListCity cityList Arrays.asList( new City(2, 上海), new City(1, 北京), new City(4, 深圳), new City(3, 广州) ); MapInteger, String cityMap cityList.stream() .collect(Collectors.toMap(City::getCityId, City::getCityName, (existing, replacement) - existing, TreeMap::new)); System.out.println(cityMap);输出结果