五、Java比较器(comparable和comparator)

tech2024-11-17  5

comparable和comparator

定义一个包含price和name的商品类,要求实现排序,价格从下到大,如果价格相同,按名称从大到小排列。

      一、comparable的使用

     首先定义一个类,并重写了compareTo方法,注意数字的比较一般是Double.conpare(A,B),字符的比较一般是A.compareTo(B)。

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 double getPrice() { return price; } public void setName(String name) { this.name = name; } 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 -this.name.compareTo(goods.name); } } throw new RuntimeException("输入数据类型不一致"); } }

接下来测试:

@Test public void test3(){ Goods[] arr = new Goods[4]; arr[0] = new Goods("lenovoMouse", 34); arr[1] = new Goods("dellMouse", 34); arr[2] = new Goods("xiaomiMouse", 12); arr[3] = new Goods("hauweiMouse", 65); System.out.println(Arrays.toString(arr)); Arrays.sort(arr); System.out.println(Arrays.toString(arr)); }

输出:

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

      二、comparator的使用

首先定义一个Goods类,但是没有重写compare方法

public 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 double getPrice() { return price; } public void setName(String name) { this.name = name; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}'; } }

然后测试:

@Test public void test3(){ Goods[] arr = new Goods[4]; arr[0] = new Goods("lenovoMouse", 34); arr[1] = new Goods("dellMouse", 34); arr[2] = new Goods("xiaomiMouse", 12); arr[3] = new Goods("hauweiMouse", 65); System.out.println(Arrays.toString(arr)); Arrays.sort(arr, new Comparator<Goods>() { @Override public int compare(Goods o1, Goods o2) { if(o1 instanceof Goods && o2 instanceof Goods){ Goods g1 = (Goods)o1; Goods g2 = (Goods)o2; if(g1.getPrice() == g2.getPrice()){ return -g1.getName().compareTo(g2.getName()); }else{ return Double.compare(g1.getPrice(),g2.getPrice()); } } throw new RuntimeException("输入数据类型不一致"); } }); System.out.println(Arrays.toString(arr)); }

输出:

[Goods{name='lenovoMouse', price=34.0}, Goods{name='dellMouse', price=34.0}, Goods{name='xiaomiMouse', price=12.0}, Goods{name='hauweiMouse', price=65.0}] [Goods{name='xiaomiMouse', price=12.0}, Goods{name='lenovoMouse', price=34.0}, Goods{name='dellMouse', price=34.0}, Goods{name='hauweiMouse', price=65.0}]
最新回复(0)