java自定义对象集合排序

一、基本对象集合排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @Package: PACKAGE_NAME
 * @ClassName: IntListTest
 * @Author: tanp
 * @Description: ${description}
 * @Date: 2020/9/8 14:42
 */
public class IntListTest {
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        Random r = new Random();
        for(int i=0; i<10; i++){
            integerList.add(r.nextInt(100));
        }
        System.out.println(integerList);
        Collections.sort(integerList);
        System.out.println(integerList);
    }

}

从上面的代码里我们可以看出,对于基本类型的数据我们可以直接调用Collections.sort();方法即可,但是对于我们自定的对象呢,如何排序呢

二、自定义对象集合排序

自定义对象集合排序有两种方法

1.自定义一个Comparator比较器

compare()方法用来给两个输入参数排序,返回负数,0,正数,表明第一个参数是小于,等于,大于第二个参数

import java.util.*;

/**
 * @ClassName: Dog
 * @Author: tanp
 * @Description: ${description}
 * @Date: 2020/9/7 17:29
 */
public class Dog {

    private String name;
    private int length;
    private int weight;

    public Dog(String name, int length, int weight) {
        this.name = name;
        this.length = length;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }


    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", length=" + length +
                ", weight=" + weight +
                '}';
    }

    public static void main(String[] args) {
        List<Dog> dogs = new ArrayList<>();
        Random r =new Random();
        for (int i = 0; i < 5; i++) {
            dogs.add(new Dog("dog"+i,r.nextInt(100), r.nextInt(100)));
        }
        System.out.println(dogs);

        //直接调用sort会出现编译错误,因为Dog有各种属性
        //到底按照哪种属性进行比较,Collections也不知道,不确定,所以没法排
        //Collections.sort(dogs);

        //引入Comparator,指定比较的算法
        Comparator<Dog> c = new Comparator<Dog>() {
            @Override
            public int compare(Dog dog1, Dog dog2) {
                //按照hp进行排序
                if(dog1.weight>=dog2.weight){
                    //正数表示h1比h2要大
                    return 1;
                }else{
                    return -1;
                }
            }
        };
        Collections.sort(dogs,c);
        System.out.println("按体重排序后的集合:");
        System.out.println(dogs);

    }
}

2.实现Comparable接口

Comparable接口里的compareTo()方法,可以个给两个对象排序。具体来说,它返回负数,0,正数,来表明已经存在的对象小于,等于,大于输入对象。

import java.util.*;

/**
 * @Package: PACKAGE_NAME
 * @ClassName: Dog1
 * @Author: tanp
 * @Description: ${description}
 * @Date: 2020/9/7 17:59
 */
public class Dog1 implements Comparable<Dog1> {

    private String name;
    private int length;
    private int weight;

    public Dog1(String name, int length, int weight) {
        this.name = name;
        this.length = length;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }


    @Override
    public String toString() {
        return "Dog1{" +
                "name='" + name + '\'' +
                ", length=" + length +
                ", weight=" + weight +
                '}';
    }

    @Override
    public int compareTo(Dog1 dog1) {
        if (weight > dog1.weight) {
            return 1;
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        List<Dog1> dogs = new ArrayList<>();
        Random r =new Random();
        for (int i = 0; i < 5; i++) {
            dogs.add(new Dog1("dog"+i,r.nextInt(100), r.nextInt(100)));
        }
        System.out.println(dogs);

        //重写了比较方法可直接调用比较方法
        Collections.sort(dogs);
        System.out.println("按体重排序后的集合:");
        System.out.println(dogs);

    }
}

三、Comparator 和 Comparable 的区别

    Comparable 定义在自定义对象类的内部,且已经实现了比较器,所以对于自定义对象而言,它现在是一个可以比较大小的对象了,它的比较功能和String完全一样,可以随时随地的拿来。Comparator 是定义在自定义对象的外部的, 此时我们的自定义对象类的类结构不需要有任何变化。

    总的来说,使用Comparable方式比较时,我们将比较的规则写入了比较的类型中,其特点是高内聚。但如果哪天这个规则需要修改,那么我们必须修改这个类型的源代码。如果使用Comparator方式比较,那么我们不需要修改比较的类,其特点是易维护,但需要自定义一个比较器,后续比较规则的修改,仅仅是改这个比较器中的代码即可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值