Java 比较器Comparable和Compartor的使用
2017-04-07 14:36
465 查看
Comparable使用
public static void main(String[] args) { demo d = new demo(); Custumer c1 = d.new Custumer(1, 1, "1"); Custumer c2 = d.new Custumer(4, 2, "2"); Custumer c3 = d.new Custumer(2, 3, "3"); Custumer c4 = d.new Custumer(2, 4, "4"); Custumer c5 = d.new Custumer(5, 5, "5"); List<Custumer> custumers = new ArrayList<demo.Custumer>(); custumers.add(c1); custumers.add(c2); custumers.add(c3); custumers.add(c4); custumers.add(c5); for(Custumer custumer:custumers){ System.out.print(" "+custumer.getName()); } System.out.println("\n----------------------"); Collections.sort(custumers); for(Custumer custumer:custumers){ System.out.print(" "+custumer.getName()); } } class Custumer implements Comparable<Custumer> { private int id; private int age; private String name; public Custumer(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int compareTo(Custumer o) { // TODO Auto-generated method stub //根据id排序,如果相同根据age排序 if (o.getId() > id) { return -1; } else if (o.getId() < id) { return 1; } else { if (o.getAge() > age) { return -1; } else if (o.getAge() < age) { return 1; } else { return 0; } } } }
Compartor使用
package test; import java.util.Comparator; /** * 具体的比较类(比较器),实现Comparator接口 * @author breeze * */ public class ComparatorConsunInfo implements Comparator<ConsumInfo> { /** * 顺序(从小到大): * if(price < o.price){ return -1; } if(price > o.price){ return 1; } * 倒序(从大到小): * if(price < o.price){ return 1; } if(price > o.price){ return -1; } */ @Override public int compare(ConsumInfo o1, ConsumInfo o2) { //首先比较price,如果price相同,则比较uid if(o1.getPrice() > o2.getPrice()){ return 1; } if(o1.getPrice() < o2.getPrice()){ return -1; } if(o1.getPrice() == o2.getPrice()){ if(o1.getUid() > o2.getUid()){ return 1; } if(o1.getUid() < o2.getUid()){ return -1; } } return 0; } } /** * 需要进行比较的类 * @author breeze * */ public class ConsumInfo{ private int uid; private String name; private double price; private Date datetime; public ConsumInfo() { // TODO Auto-generated constructor stub } public ConsumInfo(int uid,String name,double price,Date datetime){ this.uid = uid; this.name = name; this.price = price; this.datetime = datetime; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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 Date getDatetime() { return datetime; } public void setDatetime(Date datetime) { this.datetime = datetime; } @Override public String toString() { return "ConsumInfo [uid=" + uid + ", name=" + name + ", price=" + price + ", datetime=" + datetime + "]"; } } //测试类 public class ConsumInfoTest { public static void main(String[] args) { ConsumInfo consumInfo1 = new ConsumInfo(100, "consumInfo1", 400.0,new Date()); ConsumInfo consumInfo2 = new ConsumInfo(200, "consumInfo1", 200.0,new Date()); ConsumInfo consumInfo3 = new ConsumInfo(300, "consumInfo1", 100.0,new Date()); ConsumInfo consumInfo4 = new ConsumInfo(400, "consumInfo1", 700.0,new Date()); ConsumInfo consumInfo5 = new ConsumInfo(500, "consumInfo1", 800.0,new Date()); ConsumInfo consumInfo6 = new ConsumInfo(600, "consumInfo1", 300.0,new Date()); ConsumInfo consumInfo7 = new ConsumInfo(700, "consumInfo1", 900.0,new Date()); ConsumInfo consumInfo8 = new ConsumInfo(800, "consumInfo1", 400.0,new Date()); List<ConsumInfo> list = new ArrayList<ConsumInfo>(); list.add(consumInfo1); list.add(consumInfo2); list.add(consumInfo3); list.add(consumInfo4); list.add(consumInfo5); list.add(consumInfo6); list.add(consumInfo7); list.add( a0ea consumInfo8); System.out.println("排序前:"); //排序前 for(ConsumInfo consumInfo : list ){ System.out.println(consumInfo); } ComparatorConsunInfo comparatorConsunInfo = new ComparatorConsunInfo();//比较器 Collections.sort(list,comparatorConsunInfo);//排序 System.out.println("排序后:"); //排序后 for(ConsumInfo consumInfo :list){ System.out.println(consumInfo); } } }
相关文章推荐
- Java中List的排序 Comparable和 Compartor
- treeset的使用(Comparable接口和Compartor接口的使用)
- java.lang.Comparable, java.util.Compartor区别以及Hadoop中关于自定义类型中的compare方法
- java中compartor的使用方法
- Java常用类库——Arrays类(用于普通数组操作)、比较器(Comparable、Comparator 用于对象排序)的使用
- maven使用jstl表达式和The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application解决
- java比较器Comparable和Comparator的使用
- java中Comparable接口(比较器)的使用
- java内外部比较器(Comparator、Comparable)使用场景与详解
- java底层比较器Comparable和Comparator比较对象时的使用
- java comparable comparator 比较器的使用 及 自定义比较器
- Comparable和Compartor的区别
- java中比较器在结构体中的简单使用
- 站稳马步——(3)java比较器——Comparable和Comparator
- 使用internal(com.android.internal)和hidden(@hide)APIs – Part 4
- 使用internal(com.android.internal)和hidden(@hide)APIs – Part 3
- 使用internal(com.android.internal)和hidden(@hide)APIs – Part 2
- 使用internal(com.android.internal)和hidden(@hide)APIs – Part 1
- 使用 Spring 2.5 注释驱动的 IoC 功能(转自http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/)
- 演示使用 JACOB : Java COM Bridge 操纵 Word 文档