在数组的操作中,排序是比较常见的一种操作,而java中数据类型的分为基本数据类型和引用数据类型,基本数据类型可以使用关系运算符进行比较。那么如何比较引用数据类型的大小呢?此时就必须使用java提供的比较器了。java提供了两个比较器:Comparable和Comparator。
Comparable比较器
查看源码:
package java.lang;
import java.util.*;
public interface Comparable<T> {
public int compareTo(T o);
}
发现Comparable是一个接口,此接口中只有一个比较的方法:compareTo(T o),并且返回了一个int类型的值。参数为一个外部传入的对象,如果当前数据比传入的对象小返回负数;如果大,返回正数;如果相等,返回0.
要排序的对象只需要继承此接口即可。
实例:
package com.qwy;
import java.util.Arrays;
/**
* @author qwy
* @create 2021-03-23 21:45
**/
class Student implements Comparable{
private int id;
private String username;
private int age;
private int score;
public Student() {
}
public Student(int id, String username, int age, int score) {
this.id = id;
this.username = username;
this.age = age;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", username='" + username + '\'' +
", age='" + age + '\'' +
", score=" + score +
'}';
}
/**
* 此实例中首先按照学生的成绩从小到大排列,如果成绩相等则按照年龄由小到大排列
* @param o
* @return
*/
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student s= (Student) o;
if(this.score>s.getScore()) {
return 1;
} else if(this.score<s.getScore()){
return -1;
}else{
if(this.age> s.getAge()) {
return 1;
}else if(this.getAge()<s.getAge()){
return -1;
}else{
return 0;
}
}
}
return 0;
}
}
public class TestComparable {
public static void main(String[] args) {
Student[] students= new Student[]{
new Student(1,"admin",12,90),
new Student(2,"jerry",23,100),
new Student(3,"jack",32,90),
new Student(4,"tom",16,89),
new Student(5,"green",21,90),
};
System.out.println("排序之前:");
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
System.out.println("排序之后:");
Arrays.sort(students);
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}
}
运行结果:
排序之前:
Student{id=1, username=‘admin’, age=‘12’, score=90}
Student{id=2, username=‘jerry’, age=‘23’, score=100}
Student{id=3, username=‘jack’, age=‘32’, score=90}
Student{id=4, username=‘tom’, age=‘16’, score=89}
Student{id=5, username=‘green’, age=‘21’, score=90}
排序之后:
Student{id=4, username=‘tom’, age=‘16’, score=89}
Student{id=1, username=‘admin’, age=‘12’, score=90}
Student{id=5, username=‘green’, age=‘21’, score=90}
Student{id=3, username=‘jack’, age=‘32’, score=90}
Student{id=2, username=‘jerry’, age=‘23’, score=100}
注意:
Student必须实现Comparable接口,重写compareTo()方法。否则会报:java.lang.ClassCastException的异常。
Comparator比较器
从上个例子中我们发现我们要对对象进行比较,则必须实现Comparable接口,否则会出现异常。而实际开发中,可能会对未实现Comparable接口的对象进行比较,此时我们可以使用Comparator接口提供的方法进行补救。
实例:
package com.qwy;
import java.util.Arrays;
import java.util.Comparator;
/**
* @author qwy
* @create 2021-03-23 22:09
**/
class Users{
private int id;
private String username;
private int age;
private int score;
public Users() {
}
public Users(int id, String username, int age, int score) {
this.id = id;
this.username = username;
this.age = age;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
public class TestComparator {
public static void main(String[] args) {
Users[] usersArray=new Users[]{
new Users(1,"admin",12,90),
new Users(2,"jerry",23,100),
new Users(3,"jack",32,90),
new Users(4,"tom",16,89),
new Users(5,"green",21,90),
};
System.out.println("排序之前:");
for (Users u:usersArray) {
System.out.println( u);
}
/**
* 这里使用Comparator接口对象的匿名实现类创建匿名对象的方式实现
*/
Arrays.sort(usersArray, new Comparator<Users>() {
@Override
public int compare(Users u1, Users u2) {
if(u1.getScore()>u2.getScore()){
return 1;
}else if(u1.getScore()<u2.getScore()){
return -1;
}else{
if(u1.getAge()>u2.getAge()){
return 1;
}else if(u1.getAge()<u2.getAge()){
return -1;
}else{
return 0;
}
}
}
});
System.out.println("排序之后:");
for (Users u:usersArray) {
System.out.println( u);
}
}
}
运行结果:
排序之前:
Users{id=1, username=‘admin’, age=12, score=90}
Users{id=2, username=‘jerry’, age=23, score=100}
Users{id=3, username=‘jack’, age=32, score=90}
Users{id=4, username=‘tom’, age=16, score=89}
Users{id=5, username=‘green’, age=21, score=90}
排序之后:
Users{id=4, username=‘tom’, age=16, score=89}
Users{id=1, username=‘admin’, age=12, score=90}
Users{id=5, username=‘green’, age=21, score=90}
Users{id=3, username=‘jack’, age=32, score=90}
Users{id=2, username=‘jerry’, age=23, score=100}
总结:
如果一个对象的类没有实现Comparable接口,在进行比较时,我们可以使用Comparator接口提供的方法进行定制比较。