lambda表达式的list去重和list转map
public static List<Employee> createEmployee() {
List<Employee> employeeList = new ArrayList<Employee>();
Employee employee1 = new Employee();
Employee employee2 = new Employee();
Employee employee3 = new Employee();
employee1.setEmployeeId("1");
employee1.setEmployeeName("张三");
employee1.setEmployeeSex("男");
employee2.setEmployeeId("2");
employee2.setEmployeeName("王五");
employee2.setEmployeeSex("男");
employee3.setEmployeeId("3");
employee3.setEmployeeName("李四");
employee3.setEmployeeSex("男");
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
return employeeList;
}
public static List<Employee> createEmployee2() {
List<Employee> employeeList = new ArrayList<Employee>();
Employee employee1 = new Employee();
Employee employee2 = new Employee();
Employee employee3 = new Employee();
Employee employee4 = new Employee();
employee1.setEmployeeId("1");
employee1.setEmployeeName("张三");
employee1.setEmployeeSex("男");
employee2.setEmployeeId("1");
employee2.setEmployeeName("王五");
employee2.setEmployeeSex("男");
employee1.setEmployeeId("1");
employee1.setEmployeeName("田七");
employee1.setEmployeeSex("男");
employee3.setEmployeeId("3");
employee3.setEmployeeName("李四");
employee3.setEmployeeSex("男");
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
return employeeList;
}
public static List<String> createString() {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("1");
list.add("2");
return list;
}
public static void list() {
// 去重list
List<String> listString = createString();
System.out.println(listString.stream().distinct().collect(Collectors.toList()));
System.out.println("******************************************************************");
// list 转 map map的value 就是 Employee 对象
List<Employee> employeeList = createEmployee();
Map<String, Employee> map = employeeList.stream().collect(Collectors.toMap(Employee::getEmployeeId, employee -> employee));
map.forEach((k, v) -> {
System.out.println("key: " + k + " value: " + v);
});
System.out.println("******************************************************************");
// list 转 map 涉及key 重复
// 第一种写法 (k1, k2) -> k1) 的意思是保留 k1 存在重复不传第三个处理参数会报错 Duplicate key
List<Employee> employeeList2 = createEmployee2();
Map<String, String> map1 = employeeList2.stream().collect(Collectors.toMap(e -> e.getEmployeeId(), e -> e.getEmployeeName(), (k1, k2) -> k1));
System.out.println(map1);
System.out.println("******************************************************************");
// 第二种写法
Map map2 = employeeList2.stream().collect(HashMap::new, (map3, e) -> map3.put(e.getEmployeeId(), e.getEmployeeName()), Map::putAll);
System.out.println(map2);
System.out.println("******************************************************************");
// 处理遍历中存在null 的情况 value 为 null 会报空指针
// 异常重现
// Map<String, String> map3 = employeeList.stream().collect(Collectors.toMap(e -> e.getEmployeeId(), e -> e.getEmployeeBirthDay()));
// System.out.println(map3);
// 解决异常
Map<String, String> map3 = employeeList.stream().collect(Collectors.toMap(e -> e.getEmployeeId(), e -> Optional.ofNullable(e.getEmployeeBirthDay()).orElse("没有数据")));
System.out.println(map3);
System.out.println("******************************************************************");
// 当 key 全部为 null 时 而 value 唯一就会报错出现Duplicate key value的情况
// 异常重现
// Map<String, String> map4 = employeeList2.stream().collect(Collectors.toMap(Employee::getEmployeeBirthDay, Employee::getEmployeeName));
// System.out.println(map4);
// 解决异常 指定出现重复的key之后保留的字段 (解决方法很多,中心围绕在如何解决掉重复的问题,一般都是覆盖)
Map<String, String> map4 = employeeList2.stream().collect(Collectors.toMap(Employee::getEmployeeBirthDay, Employee::getEmployeeName, (k1, k2) -> k1));
System.out.println(map4);
}
public static void main(String[] args) {
list();
}
欢迎关注我的微信公众号,更多内容将在微信公众号中发布: