1 准备数据
Employee
对象:
package com.tao.springstarter.entity;
public class Employee {
/** * 姓名 */
private String name;
/** * 年龄 */
private Integer age;
/** * 所在城市 */
private String city;
/** * 销售额 */
private Double sales;
public Employee(String name, Integer age, String city, Double sales) {
this.name = name;
this.age = age;
this.city = city;
this.sales = sales;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Double getSales() {
return sales;
}
public void setSales(Double sales) {
this.sales = sales;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", city='" + city + '\'' +
", sales=" + sales +
'}';
}
}
List<Employee>
对象:
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Alice", 23, "London", 1200.00));
employeeList.add(new Employee("Bob", 19, "London", 2000.00));
employeeList.add(new Employee("Charles", 25, "New York", 1650.00));
employeeList.add(new Employee("Dorothy", 20, "Hong Kong", 1200.00));
2 List
转Map
2.1 Collectors.toMap
函数说明
List
转Map
通常使用 Collectors.toMap
函数进行转换。在Collectors
中,toMap
总共有三种形态:
2.1.1 第一种
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);
}
其中,
keyMapper
—— 指定map
中的键的获取方法。valueMapper
—— 指定map
中的值的获取方法。- 默认使用
HashMap
为Map
的实现类,当键值相同发生冲突时,抛出异常。
栗子:
(1)
// key - name, value - Employee
Map<String, Employee> map1 = employeeList.stream()
.collect(toMap(Employee::getName, Function.identity()));
(2)
// key - name, value - age
Map<String, Integer> map2 = employeeList.stream()
.collect(toMap(Employee::getName, Employee::getAge));
2.1.2 第二种(提供解决key
冲突的策略)
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);
}
其中,
keyMapper
—— 指定map
中的键的获取方法。valueMapper
—— 指定map
中的值的获取方法。mergeFunction
—— 指定key
冲突时,保存value
的策略。- 默认使用
HashMap
为Map
的实现类。
栗子:
(1)key
冲突时,舍弃value
的旧值,使用value
的新值。
// key - city, value - name
Map<String, String> map3 = employeeList.stream()
.collect(toMap(Employee::getCity, Employee::getName, (oldValue, newValue) -> newValue));
2.1.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);
}
其中,
keyMapper
—— 指定map
中的键的获取方法。valueMapper
—— 指定map
中的值的获取方法。mergeFunction
—— 指定key
冲突时,保存value
的策略。mapSupplier
—— 指定Map
的实现类。
栗子:
(1)使用LinkedHashMap
为Map
的实现类
// key - city, value - name, use LinkedHashMap
Map<String, String> map4 = employeeList.stream()
.collect(toMap(Employee::getCity, Employee::getName, (oldValue, newValue) -> newValue, LinkedHashMap::new));