编程语言
首页 > 编程语言> > Day029 Java比较器

Day029 Java比较器

作者:互联网

Java比较器


概述


方式一:自然排序(java.lang.Comparable)

例:

 public static void main(String[] args) {
        String[] arr=new String[]{"AA","CC","KK","MM","GG","JJ","DD"};

        Arrays.sort(arr);

        System.out.println(Arrays.toString(arr));
    }

输出结果

[AA, CC, DD, GG, JJ, KK, MM]

String实现了Comparable接口,因此可以排序


对于自定义的类,如果我们需要排序,可以实现Comparable接口,在compareTo()方法中指明如何排序

public 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 String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", 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 0;
            }
        }
        throw new RuntimeException("传入的数据类型不一致");
    }
}

测试程序

public static void main(String[] args) {
        Goods[] goodsArr=new Goods[4];
        goodsArr[0]=new Goods("lenovoMouse",34);
        goodsArr[1]=new Goods("dellMouse",43);
        goodsArr[2]=new Goods("xiaomiMouse",12);
        goodsArr[3]=new Goods("huaweiMouse",65);

        Arrays.sort(goodsArr);

        System.out.println(Arrays.toString(goodsArr));
    }

输出结果

[Goods{name='xiaomiMouse', price=12.0}, Goods{name='lenovoMouse', price=34.0}, Goods{name='dellMouse', price=43.0}, Goods{name='huaweiMouse', price=65.0}]

方式二:定制排序:java.util.Comparator

例1:若是想让字符串从大到小排序,我们也改不了String,这就要用到Comparator接口了

 public static void main(String[] args) {
        String[] arr=new String[]{"AA","CC","KK","MM","GG","JJ","DD"};

        Arrays.sort(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                //按照字符串从大到小的顺序排列
                if(o1 instanceof String && o2 instanceof  String)
                {
                    String s1=(String) o1;
                    String s2=(String) o2;
                    return -s1.compareTo(s2);
                }
                throw new RuntimeException("输入的数据类型不一致");
            }
        });

        System.out.println(Arrays.toString(arr));
    }

输出结果

[MM, KK, JJ, GG, DD, CC, AA]

例2:假如我不想用Goods原来实现的compareTo(),要换成按产品名称从低到高排序,若名称相同,再按价格从高到低排序,但是又不能修改原来的类Goods,就可以用Comparator接口实现

public 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 String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", 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 0;
            }
        }
        throw new RuntimeException("传入的数据类型不一致");
    }
}

测试程序

   public static void main(String[] args) {
  
        Goods[] goodsArr = new Goods[6];
        goodsArr[0] = new Goods("lenovoMouse", 34);
        goodsArr[1] = new Goods("dellMouse", 43);
        goodsArr[2] = new Goods("xiaomiMouse", 12);
        goodsArr[3] = new Goods("huaweiMouse", 65);
        goodsArr[4] = new Goods("huaweiMouse", 224);
        goodsArr[5] = new Goods("microsoftMouse", 43);

        Arrays.sort(goodsArr, 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(goodsArr));
    }

输出结果

[Goods{name='dellMouse', price=43.0}, Goods{name='huaweiMouse', price=224.0}, Goods{name='huaweiMouse', price=65.0}, Goods{name='lenovoMouse', price=34.0}, Goods{name='microsoftMouse', price=43.0}, Goods{name='xiaomiMouse', price=12.0}]

尚硅谷

标签:Goods,Java,name,price,public,排序,Day029,比较,String
来源: https://www.cnblogs.com/dwystudy/p/14825772.html