java stream 对象去重

时间:2025-04-04 15:34:39

单属性和多属性去重 

 List<User> personList = new ArrayList<User>();
        (new User("鸣人", "22", "", "1", null,null,"",""));
        (new User("鸣人", "22", "", "1", null,null,"",""));
        (new User("小樱", "22", "", "1", null,null,"",""));

        // list 一个条件 去重
        List<User> uniqueByName = ().collect(
                (
                        (() -> new TreeSet<>((User::getUsername))), ArrayList::new));
        ("一个条件去重后的list:" +(uniqueByName));

        // list 多个条件 去重
        List<User> uniqueBySalaryAndAge = ().collect(
                (
                        (() -> new TreeSet<>(
                                (p -> () + ";" + ()))), ArrayList::new));