原因:
java8 对象转 Map 时重复 key
处理:
//mergeFunction当keyMapper发生重复时处理的方法
Collectors.toMap(keyMapper, valueMapper, mergeFunction)
示例:
//1、重复时采用后面的value2 覆盖value1
List<User> userList = new ArrayList<>();
Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getName, User::getPassword, (value1, value2 ) -> value2));
//2、重复时采用后面的value2 覆盖value1
List<User> userList = new ArrayList<>();
Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getName, User::getPassword, (value1, value2 )->{return value2;}));