java比较器--Comparable与Comparetor

一、Comparable 自然排序

类实现java.lang.Comparable接口,重写compareTo方法,返回int型

package test;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Computer[] computers = new Computer[4];
        computers[0] = new Computer("Apple",10000.00);
        computers[1] = new Computer("Huawei",8000.00);
        computers[2] = new Computer("Dell",7000.00);
        computers[3] = new Computer("Lenovo",9000.00);

        if(computers[1].compareTo(computers[2]) > 0){
            System.out.println("computer1更贵");
        }else {
            System.out.println("computer2更贵");
        }

        Arrays.sort(computers);
        for (Computer c: computers) {
            System.out.println(c.getBrandName()+": ¥"+c.getPrice());
        }
    }
}

class Computer implements Comparable{
    private String brandName;
    private double price;

    @Override
    public int compareTo(Object o) {
        Computer c = null;
        if (o instanceof Computer){
            c = (Computer)o;
        }else {
            throw new RuntimeException("对象类型错误");
        }
        return (int) (this.price-c.getPrice());
    }

    public Computer(String brandName, double price) {
        this.brandName = brandName;
        this.price = price;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
// 输出:
computer1更贵
Dell:7000.0
Huawei:8000.0
Lenovo:9000.0
Apple:10000.0

二、Comparetor定制排序,临时使用

package test;

import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void main(String[] args) {
        Computer[] computers = new Computer[4];
        computers[0] = new Computer("Apple",10000.00);
        computers[1] = new Computer("Huawei",8000.00);
        computers[2] = new Computer("Dell",7000.00);
        computers[3] = new Computer("Lenovo",9000.00);

        System.out.println("********按品牌首字母升序********");
        Arrays.sort(computers, new Comparator<Computer>() {
            @Override
            public int compare(Computer o1, Computer o2) {
                return o1.getBrandName().charAt(0)-o2.getBrandName().charAt(0);
            }
        });
        for (Computer c: computers) {
            System.out.println(c.getBrandName()+": ¥"+c.getPrice());
        }

        System.out.println("********按品牌价格降序********");
        Arrays.sort(computers, new Comparator<Computer>() {
            @Override
            public int compare(Computer o1, Computer o2) {
                return (int) (o2.getPrice()-o1.getPrice());
            }
        });
        for (Computer c: computers) {
            System.out.println(c.getBrandName()+": ¥"+c.getPrice());
        }
    }
}

class Computer  {
    private String brandName;
    private double price;

    public Computer(String brandName, double price) {
        this.brandName = brandName;
        this.price = price;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
// 输出:
********按品牌首字母升序********
Apple:10000.0
Dell:7000.0
Huawei:8000.0
Lenovo:9000.0
********按品牌价格降序********
Apple:10000.0
Lenovo:9000.0
Huawei:8000.0
Dell:7000.0
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维小菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值