Comparable 和 Comparator 详解区别

版权声明:转载请注明! https://blog.csdn.net/smile_geek_sq/article/details/81987115

个人觉得这个是比较重要的,可以实现类型数组的内部排序。曾在Hadoop的一些项目中见过这俩类 尤其是 comparator.

Comparable:自然排序使用
  规则:
   this属性  参数对象o属性 比较
   this > o 正数 往后排
   this < o 负数 往前排
   this == o 0     不变

Comparator:外部比较器


Comparable 和 Comparator区别:
1.位置。
  Comparable  代码写在要比较的类中;
  Comparator                   外;
2.个数。
  Comparable  就定义一种;
  Comparator  可以定义多个比较方式。

下面上代码:

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

class Student implements Comparable<Student>{
	private int no;
	private String name;
	public Student(int no, String name) {
		this.no = no;
		this.name = name;
	}
	public int getNo() {
		return no;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "Student [no=" + no + ", name=" + name + "]";
	}
	@Override
	public int compareTo(Student o) {
		// 规则:  this  o 
		//按照学员编号 升序
/*		if(this.no > o.no) {
//			return 正数;
			return 1;
		}else if(this.no < o.no) {
//			return 负数;
			return -1;			
		}else {
			return 0;
		}*/
		return this.no - o.no;
		//降序
		/*if(this.no < o.no) {
			return 1;
		}else if(this.no > o.no) {
			return -1;			
		}else {
			return 0;
		}*/
//		return o.no - this.no;
	}

}
//自定义一个比较器
class MyComparator implements Comparator<Student>{
	@Override
	public int compare(Student o1, Student o2) {
		//o1 ,o2升序比较的规则
/*		if(o1.getNo() > o2.getNo()) {
			return 1;
		}else if(o1.getNo() < o2.getNo()) {
			return -1;
		}else {
			return 0;
		}*/
		//2降序
	/*	if(o1.getNo() < o2.getNo()) {
			return 1;
		}else if(o1.getNo() > o2.getNo()) {
			return -1;
		}else {
			return 0;
		}*/
		return o2.getNo() - o1.getNo();
	}

}

public class TestSort1 {
	public static void main(String[] args) {
		int [] arr = {11,436,2,658,22};
		Arrays.sort(arr);
		System.out.println(Arrays.toString(arr));
		//--------------------------------------------------------
		String [] strs = {"xx","aa","yy","cc","ff"};
		Arrays.sort(strs);
		for(String s : strs) {
			System.out.println(s);
		}
		//-------------------Comparable接口----------------------------------------
		Student [] stus = new Student[3];// null,null,null
		Student zhangsan = new Student(2, "zhangsan");
		Student lisi = new Student(1, "lisi");
		Student wangwu = new Student(3, "wangwu");
		stus[0] = zhangsan;
		stus[1] = lisi;
		stus[2] = wangwu;
		// Student -> Object
		//自然 升序排序 
//		Arrays.sort(stus);
		
//		MyComparator m = new MyComparator();
//		Arrays.sort(stus, m);
		//匿名内部类
		Arrays.sort(stus, new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o2.getNo() - o1.getNo();
			}
			
		});
		//Lambda
		Arrays.sort(stus, (stu1,stu2)->stu2.getNo() - stu1.getNo());
		
		for(Student stu :stus) {
			System.out.println(stu);
		}
		
	}

}

是不是清晰明了,而且你会发现像这种 用到接口的 用Lambda 有多么方便

再来一个例子

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

class Employee{
	private String name;
	private int age;
	public Employee(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + "]";
	}
	
}
public class TestSort2 {

	public static void main(String[] args) {
		Employee guojing = new Employee("yangkang", 22);
		Employee yangkang = new Employee("guojing", 20);
		Employee huangrong = new Employee("huangrong", 21);
		
		Employee [] emps = {guojing,yangkang,huangrong};
		Arrays.sort(emps, new Comparator<Employee>(){
			@Override
			public int compare(Employee o1, Employee 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;
					}
				}*/
			  if(o1.getName().compareTo(o2.getName()) == 0) {
					return o1.getAge() - o2.getAge();
			   }
			   return o1.getName().compareTo(o2.getName());
			}
			
		});
		for(Employee emp : emps) {
			System.out.println(emp);
		}
		
		
	}

}

以一言以蔽之: 用comparator接口,调用时要 把实现这个接口的类的对象传进去(或者Lambda表达式),而comparable接口不用传,默认是自然排序,想自定义排序方法需重写compareTo方法。

猜你喜欢

转载自blog.csdn.net/smile_geek_sq/article/details/81987115