Comparator.comparing嵌套对象倒序以及多重条件排序

2023-04-10 15:55:37

一、嵌套对象倒序的正确书写方式

描述:对象A内部封装对象B,根据B的字段做倒排

Comparator<User> ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge()).reversed();//报错:temp为object
Comparator<User> ageDescCom = Comparator.comparing((User temp) -> temp.getKid().getAge()).reversed();//有效
Comparator<User> ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge(), Comparator.reverseOrder());//有效

测试代码:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User extends BaseEntity {
    String nickname;
    String avatar;
    Integer gender;
    Integer age;
    User kid;

    @Override
    public User instance() {
        return new User();
    }
}

public class ComparatorTest {
    @Test
    public void test() {
        User kid = User.builder().age(1).build();
        User kid1 = User.builder().age(2).build();
        User kid2 = User.builder().age(3).build();
        User kid3 = User.builder().age(4).build();
        User kid4 = User.builder().age(5).build();

        User user = User.builder().kid(kid).build();
        User user1 = User.builder().kid(kid1).build();
        User user2 = User.builder().kid(kid2).build();
        User user3 = User.builder().kid(kid3).build();
        User user4 = User.builder().kid(kid4).build();

        List<User> list = Arrays.asList(user2,user3,user1,user,user4);
        System.out.println(list);
        //Comparator<User> ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge()).reversed();//报错:temp为object
        //Comparator<User> ageDescCom = Comparator.comparing((User temp) -> temp.getKid().getAge()).reversed();//有效
        //Comparator<User> ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge(), Comparator.reverseOrder());//有效
        list = list.stream().sorted(ageDescCom).collect(Collectors.toList());
        System.out.println(list);
    }
    
}

二、多层/嵌套/多重条件排序

描述:如:先按照年龄正序,同年龄按照体重倒序。

Comparator<XXX> A = Comparator.comparing(XXX::getAge).reversed();
Comparator<XXX> B = Comparator.comparing(XXX::getWeight);
Comparator<XXX> C = A.thenComparing(B);

测试代码:

public class ComparatorTest {

    @Test
    public void test2() {
        User kid = User.builder().age(4).weight(1).build();
        User kid1 = User.builder().age(4).weight(2).build();
        User kid2 = User.builder().age(4).weight(3).build();
        User kid3 = User.builder().age(4).weight(4).build();
        User kid4 = User.builder().age(4).weight(5).build();

        User user = User.builder().age(20).kid(kid).build();
        User user1 = User.builder().age(20).kid(kid1).build();
        User user2 = User.builder().age(20).kid(kid2).build();
        User user3 = User.builder().age(20).kid(kid3).build();
        User user4 = User.builder().age(20).kid(kid4).build();

        List<User> list = Arrays.asList(user2,user3,user1,user,user4);
        System.out.println(list);

        Comparator<User> userAgeAscCom = Comparator.comparing(User::getAge);
        Comparator<User> kidAgeAscCom = Comparator.comparing(User::getAge);
        Comparator<User> kidWeightDescCom = Comparator.comparing(temp -> temp.getKid().getWeight(), Comparator.reverseOrder());
        Comparator<User> userAgeAscThenKidWeightDescCom = userAgeAscCom.thenComparing(kidWeightDescCom);//父母年龄正序后小孩体重倒序
        Comparator<User> kidAgeAscThenKidWeightDescCom = kidAgeAscCom.thenComparing(kidWeightDescCom);//小孩年龄正序后小孩体重倒序

        list = list.stream().sorted(userAgeAscThenKidWeightDescCom).collect(Collectors.toList());
        System.out.println(list);
        list = list.stream().sorted(kidAgeAscThenKidWeightDescCom).collect(Collectors.toList());
        System.out.println(list);
    }

}
  • 作者:真名骏
  • 原文链接:https://blog.csdn.net/Cls0916/article/details/127428331
    更新时间:2023-04-10 15:55:37