比较器Comparable和Comparator

package 比较器;

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

class Employee implements Comparable<Employee>{
	private int no;
	private String name;
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(int no, String name) {
		super();
		this.no = no;
		this.name = name;
	}
	
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Employee [no=" + no + ", name=" + name + "]";
	}
	@Override
	public int compareTo(Employee o) {
		// 按员工编号升序排序
		if(this.no==o.no) {
			return 0;
		}else if(this.no >o.no) {
			return 1;//正数
		}else {
			return -1;//负数
		}
//		return this.no- o.no;
	}
}
//自己定义比较器
class MyComparator implements Comparator<Employee>{

	@Override
	public int compare(Employee o1, Employee o2) {
		// TODO Auto-generated method stub
		if(o1.getNo()==o2.getNo()) {
			return 0;
		}else if(o1.getNo()>o2.getNo()) {
			return 1;//正数
		}else {
			return -1;//负数
		}
	}
	
}
public class TestArrays {

	public static void main(String[] args) {
		// 基本数据类型
		/*int []arr= {11,33,66,34,23};
		Arrays.sort(arr);
		System.out.println(Arrays.toString(arr));
		Arrays.stream(arr).forEach(System.out::println);*/
		
		//排序;字典自然生序
		/*String []strs= {"aa","yy","cc","ff"};
		Arrays.sort(strs);
		System.out.println(strs.toString());
		Arrays.stream(strs).forEach(System.out::println);*/
		
		//
		Employee zhangsan =new Employee(11,"张三");
		Employee lisi =new Employee (33,"李四");
		Employee wangwu =new Employee(22,"王五");
		Employee []emps= {zhangsan,lisi,wangwu};
		//
//		Arrays.sort(emps);
		
		
		/*Arrays.sort(emps,new Comparator<Employee>() {

			@Override
			public int compare(Employee o1, Employee o2) {
				// 按编号降序
				return o2.getNo()-o1.getNo();
			}
			
		});
		Arrays.sort(emps,(e1,e2)->e2.getNo()-e1.getNo());*/
		
		/*Arrays.sort(emps,new Comparator<Employee>() {

			@Override
			public int compare(Employee o1, Employee o2) {
				//按名字升序
				if(o1.getName().compareTo(o2.getName())==0) {
					return 0;
				}else if(o1.getName().compareTo(o2.getName())>0) {
					return 1;
				}else {
					return -1;
				}
				return o1.getName().compareTo(o2.getName());
			}
			
		});*/
		
//		Arrays.sort(emps,(o1,o2)->o1.getName().compareTo(o2.getName()));
		
		
		Arrays.sort(emps, new MyComparator());
		Arrays.stream(emps).forEach(System.out::println);
		
	}

}
package 比较器;

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

//定义一个学生类
class Student implements Comparable<Student>{
	private int score;
	private String name;
	private int age;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(int score, String name, int age) {
		super();
		this.score = score;
		this.name = name;
		this.age = age;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	//重写tostring
	@Override
	public String toString() {
		return "Student [score=" + score + ", name=" + name + ", age=" + age + "]";
	}
	@Override
	public int compareTo(Student o) {
		// 按分数降序排列
		/*if(this.getScore()==o.score) {
			return 0;
		}else if(this.getScore()>o.score) {
			return -1;
		}else {
			return 1;
		}*/
		return o.score-this.getScore();
	}
	
}
public class TestComparable {

	public static void main(String[] args) {
		Student zhangsan=new Student(100,"张三",22);
		Student lisi = new Student (80,"李四",23);
		Student wangwu = new Student(90,"王五",20);
		Student []stus= {zhangsan,lisi,wangwu};
//		System.out.println(Arrays.toString(stus));
		//重写排序
//		Arrays.sort(stus);
		Arrays.sort(stus,new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				//方法一 按名字排序 名字相同按年龄
				/*if(o1.getName().compareTo(o2.getName())>0) {
					return 1;
				}else if(o1.getName().compareTo(o2.getName())<0) {
					return -1;
				}else {
					if(o1.getAge()>o2.getAge()) {
						return 1;
					}else if(o1.getAge()<o2.getAge()) {
						return -1;
					}else {
						return 0;
					}	
				}*/
				//方法二
				int n=o1.getName().compareTo(o2.getName());
				if(n==0) {
					return o1.getAge()-o2.getAge();
				}else {
					return n;
				}
			}
		});
		Arrays.stream(stus).forEach(System.out::println);
		
		
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值