万网主机 wordpress,手机网站关键词seo,广州网站建设 企业,wordpress 访问量过大前言
总所周知,java对象的比较有 三种方式
最简单的是可以调用equals().
因为这个方法定义在Object类中#xff0c;而我们的类都继承了Object类.所以我们自己定义的类都可以使用这个方法.
除此以外还有两个比较接口,可以通过实现他们的某些方法比较我们的对象 他们是 Com…前言
总所周知,java对象的比较有 三种方式
最简单的是可以调用equals().
因为这个方法定义在Object类中而我们的类都继承了Object类.所以我们自己定义的类都可以使用这个方法.
除此以外还有两个比较接口,可以通过实现他们的某些方法比较我们的对象 他们是 Comparable 和 Comparator! Comparable 接口 方法: int compareTo(T o)用途: 定义对象的自然排序。实现: 当一个类实现 Comparable 接口时它必须重写 compareTo() 方法。这个方法用于比较当前对象与指定对象的顺序。
光这么说可能有点抽象,我们直接看代码
class Person implements ComparablePerson {public String name;public int age;public Person(String name, int age) {this.name name;this.age age;}public int getAge() {return age;} Overridepublic int compareTo(Person other) {return Integer.compare(this.age, other.age); // 按年龄升序排序}Overridepublic String toString() {return name ( age );}
} // 使用示例ListPerson people new ArrayList();people.add(new Person(Alice, 30));people.add(new Person(Bob, 25));people.add(new Person(Charlie, 35));Collections.sort(people);System.out.println(people); // 输出: [Bob (25), Alice (30), Charlie (35)] 通过这个代码我们可以看到, 我首先创建了一个Person类,接入了 Comparable 接口
又重写了compareTo 这个方法
这里有一个问题我想提一嘴,为什么用的是包装类Integer 去比较,而不是int
答: Integer 是一个对象: Integer 类提供了比较的方法比如 compareTo() 和静态方法 Integer.compare(), 这些方法可以直接用于比较两个 Integer 对象。 int 是基本类型: 基本数据类型没有方法因此不能直接调用比较函数。虽然你可以使用运算符如 或 来比较 int 值但在需要使用排序或集合等高级操作时必须依赖于对象的方法。
我们可以看看Integer的 代码
public final class Integer extends Number implements ComparableInteger 我们可以得出结论:
Integer 类实现了 Comparable 接口允许你比较两个 Integer 对象。这使得 Integer 可以使用 compareTo() 方法进行自然排序。
而我们的int就没有这个待遇了
它的特点
侵入性很强,直接写在类里面了,每次使用都会是这个比较原则
Comparator比较器
如果去使用 这个接口,我们需要一个比较器对象,实现这个接口
class NameComparator implements ComparatorPerson
{Overridepublic int compare(Person o1, Person o2) {return o1.name.compareTo(o2.name);}
} 还是刚刚的类,刚刚的例子 ListPerson people2 new ArrayList();people2.add(new Person(Alice, 30));people2.add(new Person(Charlie, 35));people2.add(new Person(Bob, 25));Collections.sort( people2, new NameComparator());System.out.println( people2); // 输出: [Alice (30), Bob (25), Charlie (35)]
它的特点
非侵入性,即不会影响类本身的排序原则。 关于Collections
我们这里使用了Collections,这是一个工具类,有sort方法
我们看看它的代码 public static T extends Comparable? super T void sort(ListT list) {list.sort(null);}public static T void sort(ListT list, Comparator? super T c) {list.sort(c);}Collections类中的sort()方法可以接收实现了List接口的任何集合如ArrayList和LinkedList并支持使用Comparable接口或Comparator比较器对其元素进行排序。这使得我们能够灵活地对不同类型的列表进行排序便于管理和操作数据。 完整代码 import java.util.*;
class Person implements ComparablePerson {public String name;public int age;public Person(String name, int age) {this.name name;this.age age;}public int getAge() {return age;} Overridepublic int compareTo(Person other) {return Integer.compare(this.age, other.age); // 按年龄升序排序}Overridepublic String toString() {return name ( age );}
}
class NameComparator implements ComparatorPerson
{Overridepublic int compare(Person o1, Person o2) {return o1.name.compareTo(o2.name);}
}
public class Com
{public static void main(String[] args){// 使用示例ListPerson people new ArrayList();people.add(new Person(Alice, 30));people.add(new Person(Bob, 25));people.add(new Person(Charlie, 35));Collections.sort(people);System.out.println(people); // 输出: [Bob (25), Alice (30), Charlie (35)]ListPerson people2 new ArrayList();people2.add(new Person(Alice, 30));people2.add(new Person(Charlie, 35));people2.add(new Person(Bob, 25));Collections.sort( people2, new NameComparator());System.out.println( people2); // 输出: [Alice (30), Bob (25), Charlie (35)]}
}结尾 本文算是笔者的第二篇备忘录吧,学习路上对于细节知识笔者总是喜欢刨根问底,为了不浪费幸苦得出的知识,笔者写下这篇博客供大家伙也参考参考