您的位置:首页 > 编程语言 > Java开发

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