2.3行为参数---用Comparator 来排序

行为参数中入参为对象,通过比较对象属性来对比排序

---->当比较的是入参的属性类型为String/Integer/BigDecimal时:a.getColor().compareTo(b.getColor());

---->当比较的是入参的属性类型为int(Integer)时:

public class ConsultAppleByWeight {
    public static void main(String[] args) {
        
        List<Apple> apples = Arrays.asList(new Apple("red", 80),
                   new Apple("green", 188),
                   new Apple("green", 200),
                   new Apple("red", 199));
        /*apples.sort(new Comparator<Apple>() {
             public int compare(Apple a1, Apple a2){
             return a1.getWeight() - (a2.getWeight());
             }
            });*/
        //apples.sort((Apple a, Apple b) -> a.getColor().compareTo(b.getColor()));
       apples.sort((Apple a, Apple b) -> a.getWeight() - b.getWeight());//当类型为int/Integer时
       apples.sort((Apple a, Apple b) -> a.getWeight().compareTo(b.getWeight()));//当类型为Integer时
        System.out.println(apples);
    }
    -------Int(Integer)对象与对象属性类型为int(Integer)的排序的区别--------------------------
    @Test
    public void sss() {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(1);
        arrayList.add(5);
        arrayList.add(3);
        arrayList.add(2);
//        arrayList.sort((a,b) -> a.compareTo(b));
//        Collections.sort(arrayList,(a,b) -> a.compareTo(b));
        Collections.sort(arrayList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println(arrayList);
    }
}

有什么有误区的地方,请各位道友指正!

转载于:https://my.oschina.net/u/3589048/blog/1204531

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。Comparator接口是Java中用于比较两个对象的接口,可以用于排序等操作。如果我们想要对一个对象列表进行排序,可以使用Collections.sort()方法,并且指定一个Comparator对象作为参数来实现排序。下面是一个使用Comparator接口进行排序的示例代码: 假设我们有一个Student类,有两个属性,分别是姓名和年龄。 ```java public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } ``` 现在我们有一个Student对象列表,我们想要按照年龄升序排序,可以使用以下代码: ```java List<Student> studentList = new ArrayList<>(); studentList.add(new Student("张三", 18)); studentList.add(new Student("李四", 20)); studentList.add(new Student("王五", 19)); Collections.sort(studentList, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }); ``` 在上面的代码中,我们创建了一个Comparator对象,实现了compare方法,该方法接收两个Student对象作为参数,并返回一个int值,表示比较的结果。如果返回一个负数,则表示o1比o2小;如果返回一个正数,则表示o1比o2大;如果返回0,则表示o1等于o2。最后,我们调用Collections.sort()方法,并将studentList和Comparator对象作为参数传入,即可对列表进行排序。 希望这个示例可以帮助你理解如何使用Comparator接口进行排序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值