比较器 compareTo compare 自定义类 内部比较器 外部比较器
int类型
比较的思路:将比较的数据做差,然后返回一个int类型的数据,将这个int类型的数值 按照 =0 >0 <0
int ai = 10;
int bi = 20;
System.out.println(ai-bi); // =0 >0 <0
String
compareTo
-
int
compareTo(String anotherString)
按字典顺序比较两个字符串。- 两个字符串相等,则结果为 0;
- 两个字符串在位置 k 处不同,返回两个char 值的差
this.charAt(k)-anotherString.charAt(k)
- 没有字符不同的索引位置,返回这两个字符串长度的差,即值:
this.length()-anotherString.length()
相关源码
public int compareTo(String anotherString) { int len1 = value.length; // 本字符串长度 int len2 = anotherString.value.length; int lim = Math.min(len1, len2); //取较短字符串长度 char v1[] = value; //本字符串给 v1字符数组 char v2[] = anotherString.value; //外字符串给 v1字符数组 int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; //不同位置字符的ASCII码差值 } k++; } return len1 - len2; //返回结果为两字符串长度差值 }
String s = new String("abc");
System.out.println(s.compareTo("abc")); //0
System.out.println(s.compareTo("ab")); //1
System.out.println(s.compareTo("abd")); //-1
Double
compareTo
int
compareTo(Double anotherDouble)
对两个 Double 对象所表示的数值进行比较。
-
anotherDouble - 要比较的 Double 值。
-
Specified by:
compareTo
in interfaceComparable<Double>
-
在应用到基本 double 值时,有两种方法可以比较执行此方法生成的值与执行 Java 语言数字比较运算符(<、<=、== 和 >= >)生成的值之间的区别这,可以确保受此方法影响的 Double 对象的自然顺序与 equals 一致。
-
方法等于自身并大于所有其他double 值(包括 Double.POSITIVE_INFINITY)
-
方法认为 0.0d 大于 -0.0d。
-
返回:
-
如果此 Double在数字上等于 anotherDouble ,则返回 0;
-
如果此 Double 在数字上小于 anotherDouble,则返回小于 0 的值;
-
如果此 Double 在数字上大于此 anotherDouble,则返回大于 0 的值。
compare
public static int compare(double d1, double d2)
比较两个指定的双精度值。
-
返回的整数值的符号与调用将返回的整数的符号相同:
new Double(d1).compareTo(new Double(d2))
-
d1 - 要比较的第一个双精度值, d2 - 第二个 double to compare
返回:
- 如果 d1 在数字上等于 d2,则值为 0;
- 如果 d1 在数值上小于 d2,则为小于 0 的值;
- 如果 d1 在数值上大于 d2,则该值大于 0。
相关源码
public int compareTo(Double anotherDouble) {
return Double.compare(value, anotherDouble.value);
}
public static int compare(double d1, double d2) {
if (d1 < d2)
return -1; // 两个val都不是NaN,这个val更小
if (d1 > d2)
return 1; // 两个val都不是NaN,这个val更大
// 由于可能出现NAN,无法使用doubleToRawLongBits。
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // 值相等
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
}
double a = 8.6;
double b = 6.3;
/* System.out.println((int)(a-b));*/
System.out.println(((Double) a).compareTo((Double) b));
自定义的数据类型
内部比较器
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
//按照年龄进行比较:
// return this.getAge() - o.getAge();
//按照名字比较:
return this.getName().compareTo(o.getName());
}
}
class TestStudent{
public static void main(String[] args) {
//比较两个学生:
Student s1 = new Student("fyz",19);
Student s2 = new Student("yjk",19);
System.out.println(s1.compareTo(s2));
}
}
外部比较器
外部比较器利用多态,扩展性好
package com.compare.wai;
import java.util.Comparator;
/**
* @author: SKPrimin
* @date: 2021/8/18 7:13
* @ClassName: Student
* @Description: TODO
*/
public class Student {
private String name;
private int age;
private double height;
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 double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
比较类
class CompareAge implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
//比较年龄:
return o1.getAge() - o2.getAge();
}
}
class CompareName implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
//比较姓名:
return o1.getName().compareTo(o2.getName());
}
}
class CompareHeight implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
//比较身高:
return Double.compare((o1.getHeight()), (o2.getHeight()));
}
}
class CompareAgetoHeight implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
//在年龄相同的情况下比较身高 年龄不同比较年龄
if ((o1.getAge() - o2.getAge()) == 0) {
//年龄相同,比较身高
return Double.compare((o1.getHeight()), (o2.getHeight()));
} else {
//年龄不同
return o1.getAge() - o2.getAge();
}
}
}
测试类
class Test02 {
//这是main方法,程序的入口
public static void main(String[] args) {
//比较两个学生:
Student s1 = new Student("fyz",19,180.5);
Student s2 = new Student("yjk",19,178.3);
//获取外部比较器:
CompareAge cpa = new CompareAge();
System.out.println(cpa.compare(s1, s2)); //0
CompareName cpn = new CompareName();
System.out.println(cpn.compare(s1, s2)); //-19
CompareHeight cph = new CompareHeight();
System.out.println(cph.compare(s1, s2)); //1
CompareAgetoHeight cpah = new CompareAgetoHeight();
System.out.println(cpah.compare(s1, s2)); //1
}
}