java排序Comparable和Comparator实际用法

java排序Comparable和Comparator实际用法


Comparable和Comparator都是用来实现集合中元素的比较、排序的,下面是两种比较的写法

一、Comparable实际使用?

Comparable接口的使用举例: 自然排序
像String、包装类等实现了Comparable接口,重写了compareTo(obj)方法,给出了比较两个对象大小的方式。
重写compareTo(obj)的规则:
* 如果当前对象this大于形参对象obj,则返回正整数,
* 如果当前对象this小于形参对象obj,则返回负整数,
* 如果当前对象this等于形参对象obj,则返回零。。

代码如下(示例):

class Goods implements Comparable {
    private String name;
    private double price;
    public Goods() {
    }
    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    //指明商品比较大小的方式:按照价格从低到高排序,再按照产品名称从高到低排序
    @Override
    public int compareTo(Object o) {
        if (o instanceof Goods) {
            Goods goods = (Goods) o;
            if (this.price > goods.price) {
                return 1;
            } else if (this.price < goods.price) {
                return -1;
            } else {
                return -this.name.compareTo(goods.name);
            }
        }
        throw new RuntimeException("传入的数据类型不一致!");
    }
}
 public static void main(String[] args) {
    Goods[] arr = new Goods[5];
    arr[0] = new Goods("lenovoMouse", 34);
    arr[1] = new Goods("dellMouse", 43);
    arr[2] = new Goods("xiaomiMouse", 12);
    arr[3] = new Goods("huaweiMouse", 65);
    arr[4] = new Goods("microsoftMouse", 43);
    //排序操作
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
  }

二、Comparator实际使用?

Comparator接口的使用:定制排序
重写compare(Object o1,Object o2)方法,比较o1和o2的大小:
* 如果方法返回正整数,则表示o1大于o2;
* 如果返回0,表示相等;
* 返回负整数,表示o1小于o2

class Goods {

    private String name;
    private double price;

    public Goods() {
    }
    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
   
}
public static void main(String[] args) {
        Goods[] arr = new Goods[6];
        arr[0] = new Goods("lenovoMouse", 34);
        arr[1] = new Goods("dellMouse", 43);
        arr[2] = new Goods("xiaomiMouse", 12);
        arr[3] = new Goods("huaweiMouse", 65);
        arr[4] = new Goods("huaweiMous1e", 224);
        arr[5] = new Goods("microsoftMouse", 43);

        Arrays.sort(arr, new Comparator() {
            //指明商品比较大小的方式:按照产品名称从低到高排序,再按照价格从高到低排序
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Goods && o2 instanceof Goods) {
                    Goods g1 = (Goods) o1;
                    Goods g2 = (Goods) o2;
                    if (g1.getName().equals(g2.getName())) {
                        return Double.compare(g1.getPrice(), g2.getPrice());
                    } else {
                        return g1.getName().compareTo(g2.getName());
                    }
                }
                throw new RuntimeException("输入的数据类型不一致");
            }
        });

        System.out.println(Arrays.toString(arr));
    }
上一篇:JavaWeb在线商城系统(功能设计、数据库设计、代码编写)


下一篇:微信小程序秒杀