本文主要是介绍comparable和comparator的区别(代码实现比较两个person),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
两者都是java的比较器。不过 comparable,需要被比较的类自己去实现排序逻辑;comparator不需要。
Comparator和Comparable的区别
参数 | Comparable | Comparator |
---|---|---|
排序逻辑 | 排序逻辑必须在待排序对象的类中,故称之为自然排序 | 排序逻辑在另一个实现 |
实现 | 实现Comparable接口 | 实现Comparator接口 |
排序方法 | int compareTo(Object o1) | int compare(Object o1,Object o2) |
触发排序 | Collections.sort(List) | Collections.sort(List, Comparator) |
接口所在包 | java.lang.Comparable | java.util.Comparator |
----------
实现比较person,先按年龄排,年龄相同的按工资排。
------
1.comparable 实现。(person类实现Comparable接口,实现比较方法compareTo,比较时使用Conllections.sort(list)即可 )
package com.fjf.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* 2018年7月14日 23:22:21
* @author FJf
*
* 比较大小 ,按年龄从小到大,年龄相同的;按工资多少排
* Comparable 需要 被比较的对象 自己实现比较的方法
*/
public class TestComparable {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("张三", 50,"男",500.00));
list.add(new Person("李四", 27,"女",800.00));
list.add(new Person("王五", 33,"男",500.00));
list.add(new Person("赵六", 33,"男",1000.00));
list.add(new Person("付8", 33,"男",500.00));
for (Person p : list) {
System.out.println(p);
}
System.out.println("--------Comparable比较----------");
Collections.sort(list);
for (Person p : list) {
System.out.println(p);
}
}
}
class Person implements Comparable<Person>{
private String name;
private int age;
private String sex;
private double salary;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Person(String name, int age, String sex,double salary) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.salary = salary;
}
public Person() {
super();
}
@Override
public int compareTo(Person o) {
int ageCompare = this.age - o.age;
if(ageCompare==0){ //年龄相同 用工资排
int salaryCompare = (this.salary - o.salary)>0.00?-1:1; //工资如果再相同就不管了
System.out.println(salaryCompare);
return salaryCompare;
}
return ageCompare;
//return o.age -this.age;
//this.compareTo(o.age);
//this.getAge().compareTo(o.getAge());
//this.name.compareTo(anotherString)
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex
+ ", salary=" + salary + "]";
}
}
--------------------------------
2.comparator实现。(pesron类自己无需实现任何比较逻辑。在真正比较时,Conllections.sort(list,Comparator)在传入一个comparator比较器实现比较逻辑)
package com.fjf.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* 2018年7月14日 23:42:30
* @author fjf
*
* 使用comparator比较
*/
public class TestComparator {
public static void main(String[] args) {
List<Person2> list = new ArrayList<Person2>();
list.add(new Person2("张三", 50,"男",500.00));
list.add(new Person2("李四", 27,"女",800.00));
list.add(new Person2("王五", 33,"男",500.00));
list.add(new Person2("赵六", 33,"男",1000.00));
list.add(new Person2("付8", 33,"男",500.00));
for (Person2 p : list) {
System.out.println(p);
}
System.out.println("---------Comparator比较----------");
Collections.sort(list, new Comparator<Person2>() {
@Override
public int compare(Person2 o1, Person2 o2) {
int ageCompare = o1.getAge()-o2.getAge();
if(ageCompare == 0){
int salaryCompare = (o1.getSalary()-o2.getSalary()>0.00?1:-1);
return salaryCompare;
}
return ageCompare;
}
});
for (Person2 p : list) {
System.out.println(p);
}
}
}
class Person2 {
private String name;
private int age;
private String sex;
private double salary;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Person2(String name, int age, String sex, double salary) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.salary = salary;
}
public Person2() {
super();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex
+ ", salary=" + salary + "]";
}
}
---
附:网友的一些介绍。
https://blog.csdn.net/u013256816/article/details/50899416/
https://blog.csdn.net/u014133299/article/details/78608454
这篇关于comparable和comparator的区别(代码实现比较两个person)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!