投资做网站,电子商务网站建设的基本步骤,中华室内设计网怎么样,什么能建我的网站呢String类的Comparable接口
1、String类实现了ComparableString接口#xff0c;并提供了compareTo方法的实现#xff0c;因此#xff0c;字符串对象#xff08;即String类型的实例#xff09;可以直接调用compareTo()方法来比较它们。2、String类的compareTo()方法…String类的Comparable接口
1、String类实现了ComparableString接口并提供了compareTo方法的实现因此字符串对象即String类型的实例可以直接调用compareTo()方法来比较它们。2、String类的compareTo()方法是这样工作的它按照字典顺序比较两个字符串从两个字符串的第一个字符开始比较如果它们相等则继续比较下一个字符直到找到不同的字符或者到达字符串的末尾如果所有的字符都相同那么较短的字符串被认为是较小的。 排序
Comparable接口 实现Comparable接口 就必须重写它的compareTo()方法Comparable接口是一个泛型接口在类的声明中使用泛型参数来指定需要比较的对象类型它包含一个compareTo()方法如下所示public interface ComparableT {int compareTo(T o);}compareTo()方法返回一个整数值用于表示当前对象与另一个对象的比较结果。通常它有以下三种返回值如果当前对象 小于 另一个对象则返回负整数。如果当前对象 等于 另一个对象则返回零。如果当前对象 大于 另一个对象则返回正整数。使用的场景1、具体的类A实现Comparable接口2、重写Comparable接口中的compareTo(Object obj)方法在此方法中指明比较类A的对象的大小的标准3、创建类A的多个实例进行大小的比较或排序。
实现Comparable接口
要使一个类可以进行自然排序需要实现Comparable接口并提供compareTo()方法的具体实现在compareTo()方法中你需要指定对象之间的比较规则如
Student.java
public class Student implements ComparableStudent {private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}Overridepublic int compareTo (Student other){return this.age - other.age;}public String toString() {return Student{name name , age age };}
}
Student类实现了ComparableStudent接口并重写了compareTo()方法按照年龄升序排序是通过比较当前对象的年龄属性和另一个对象的年龄属性来实现的。MyTest.java
import java.util.TreeSet;
public class MyTest {public static void main(String[] args) {TreeSetStudent studentSet new TreeSet();studentSet.add(new Student(Alice, 22));studentSet.add(new Student(Bob, 20));studentSet.add(new Student(Charlie, 25));for (Student student : studentSet) {System.out.println(student);}}
}
/* 打印结果如下Student{nameBob, age20}Student{nameAlice, age22}Student{nameCharlie, age25}
*/对上面的代码做一下解释在Java中当你创建一个实现了Comparable接口的类的实例并将其放入一个TreeSet集合时TreeSet会自动使用该类实现的compareTo()方法来对集合中的元素进行排序你不需要显式地调用compareTo()方法因为TreeSet内部在需要时会自动调用它。另1、创建TreeSet实例当你创建 TreeSetStudent studentSet 时实际上是在告诉Java你想要一个有序的、不重复的集合该集合将包含Student类型的对象。2、添加元素到TreeSet当你调用 studentSet.add(new Student(Alice, 22)) 等方法添加元素时TreeSet内部会检查新元素与集合中已存在元素的顺序关系这是通过调用新元素的compareTo方法实现的。3、自动排序在TreeSet内部元素是以红黑树的结构存储的当你添加一个新元素时TreeSet会使用compareTo()方法来确定新元素在树中的正确位置以保持集合的有序性这个过程是自动的你不需要显式地调用任何排序方法。4、遍历TreeSet当你遍历TreeSet时使用for-each循环你会看到元素已经按照compareTo方法定义的顺序排列好了。这个例子中Student类实现了ComparableStudent接口并重写了compareTo方法该方法比较两个Student对象的age属性因此当你将Student对象添加到TreeSet时它们会按照年龄升序排列。
Collections.sort
Student.java
public class Student implements ComparableStudent{private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}Overridepublic int compareTo(Student other) {return this.age - other.age;}public String toString() {return Student{name name , age age };}
}import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test1 {public static void main(String[] args) {ListStudent list new ArrayList();list.add(new Student(Alice, 22));list.add(new Student(White, 18));list.add(new Student(Black, 30));// 使用了Collections.sort()方法对学生列表进行了排序Collections.sort(list);for (Student stu : list){System.out.println(stu);}/*打印结果为Student{nameWhite, age18}Student{nameAlice, age22}Student{nameBlack, age30}* 使用了Collections.sort()方法对学生列表进行了排序* 因为Student类实现了 Comparable 接口* 所以它根据年龄属性自动进行了升序排序。* */}
}为什么 Collections.sort(list) 会使用 Student类的compareTo()方法进行排序呢因为Student类实现了ComparableStudent接口并重写了compareTo()方法所以当你调用 Collections.sort(list)时该方法会自动调用Student类中的compareTo()方法来比较对象并进行排序。
自定义排序
要求有时需要对对象进行多属性排序例如先按年龄升序排序然后按姓名字母顺序排序为了实现多属性排序可以在 compareTo()方法中逐一比较不同属性确保按照所需顺序比较。Person.java
public class Person implements ComparablePerson{String name;int age;public Person(String name, int age) {this.name name;this.age age;}Overridepublic int compareTo(Person other) {// 先按年龄升序int ageComparison this.age - other.age;if (ageComparison ! 0) {return ageComparison;}// 如果年龄相等则按照名字字母排序return this.name.compareTo(other.name);}Overridepublic String toString() {return Student{name name , age age };}
}import java.util.Collections;
import java.util.List;
public class PersonTest {public static void main(String[] args) {ListPerson list new ArrayList();list.add(new Person(Wang, 26));list.add(new Person(King, 19));list.add(new Person(He, 19));list.add(new Person(Black, 28));/*为了对列表进行排序需要调用Collections.sort()方法并传递您的Person列表作为参数因为Person类实现了ComparablePerson接口并定义了比较逻辑所以Collections.sort()方法将能够使用Person类中的compareTo()方法来对列表进行排序。*/Collections.sort(list);for (Person per : list) {System.out.println(per);}/** 打印结果为Student{nameHe, age19}Student{nameKing, age19}Student{nameWang, age26}Student{nameBlack, age28}* */}
} Comparator定制排序
什么时候使用当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码(比如JDK当中的类)或者实现了java.lang.Comparable接口但定义好的排序规则不适合当前的操作那么可以考虑使用接口Comparator的对象来排序强行对多个对象进行整体排序的比较。1、public interface ComparatorTComparator属于接口且支持范型位于java.util包下2、Comparator接口内置实现自定义排序的抽象方法compare()int compare(T o1,T o2)T泛型这个方法是让两个形参对象去比较大小。重写compare(Object o1,Object o2)方法比较o1和o2的大小如果返回正整数则表示o1大于o2如果返回0表示相等如果返回负整数表示 o1小于o2需要在compare中指明o1和o2按照什么规则比较3、可以将Comparator传递给sort方法如 Collections.sort或Arrays.sort从而允许在排序顺序上实现精确控制。还可以使用 Comparator 来控制某些数据结构如有序set 或 有序映射的顺序或者为那些没有自然顺序的对象 collection 提供排序。
Arrays类下的sort方法
public static T void sort(T[] a, Comparator? super T c)
对基本数据类型的排序
import java.util.Arrays;
import java.util.Comparator;public class TestComparator {public static void main(String[] args) {Integer[] arr1 {1,4,2,3};// 降序Arrays.sort(arr1, new ComparatorInteger() {Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1;}});System.out.println(Arrays.toString(arr1)); // [4, 3, 2, 1]// 升序Arrays.sort(arr1, new ComparatorInteger() {Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;}});System.out.println(Arrays.toString(arr1)); // [1, 2, 3, 4]}
}
对象数组的排序
StuComparator.java
public class StuComparator {String name;int age;double height;public StuComparator() {}public StuComparator(String name, int age, double height) {this.name name;this.age age;this.height 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;}Overridepublic String toString() {return \n name name , age age , height height \n;}
}TestStuComparator.java
import java.util.Arrays;
import java.util.Comparator;
public class TestStuComparator {public static void main(String[] args) {StuComparator[] stu new StuComparator[4];stu[0] new StuComparator(张三, 16, 176.6);stu[1] new StuComparator(李四, 25, 181.3);stu[2] new StuComparator(小明, 18, 179.4);stu[3] new StuComparator(小红, 17, 165);// 以年龄排序Arrays.sort(stu, new ComparatorStuComparator() {Overridepublic int compare(StuComparator o1, StuComparator o2) {return o1.age - o2.age;}});System.out.println(Arrays.toString(stu));/*[name张三, age16, height176.6,name小红, age17, height165.0,name小明, age18, height179.4,name李四, age25, height181.3]*/// 以身高排序Arrays.sort(stu, new ComparatorStuComparator() {Overridepublic int compare(StuComparator o1, StuComparator o2) {return Double.compare(o1.height, o2.height);}});System.out.println(Arrays.toString(stu));/*[name小红, age17, height165.0,name张三, age16, height176.6,name小明, age18, height179.4,name李四, age25, height181.3]*/}
}