Numeric data (byte int short long float double) It is naturally comparable in size and sortable. String implements the Comparable interface and can also compare sizes and sort. However, there are many kinds of custom classes, and there is no common indicator that can be used as a sorting indicator. Therefore, it is necessary to manually establish comparisons in the custom class. Method, for this purpose, java provides two interfaces Comparable and Comparator.
Collections.sort() underlying sorting relies on Arrays.sort(), and Arrays.sort() The bubble method is used for sorting.
Objects that need to be compared in size can implement the Comparable interface and implement the abstract method in it, which is used to set the comparison The way. The following is an example to illustrate:
package com.javase.collections.comparable;public class Student implements Comparable<Student> {private String name;private int score;public Student() {super(); }public Student(String name, int score) {super();this.name = name;this.score = score; }public String getName() {return name; }public void setName(String name) {this.name = name; }public int getScore() {return score; }public void setScore(int score) {this.score = score; } @Overridepublic int compareTo(Student stu) {return this.score - stu.score;// 操作对象减去参数对象,升序排列,反之降序。 } }
In the compareTo() method, use The attribute score is the sorting indicator, using "this.score-stu.score". The final results are arranged in ascending order, and vice versa.
package com.javase.collections.comparable;import java.util.ArrayList;import java.util.Collections;import java.util.List;import org.junit.Test;public class ComparableTest { @Testpublic void testComparable() { List<Student> stus = new ArrayList<Student>(); Student zhangsan = new Student("zhangsan", 100); Student lisi = new Student("lisi", 90); Student wanger = new Student("wanger", 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); System.out.println("排序前");for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } System.out.println("排序后"); Collections.sort(stus);for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } } }
Output:
If a class does not implement the Comparable interface when it is created, you want to object to it without modifying the source code To sort, you can implement the Comparator interface and specify the sorting method when calling the sorting method. The following is an example to illustrate:
package com.javase.collections.comparator;public class Student {private String name;private int score;public Student() {super(); }public Student(String name, int score) {super();this.name = name;this.score = score; }public String getName() {return name; }public void setName(String name) {this.name = name; }public int getScore() {return score; }public void setScore(int score) {this.score = score; } }
package com.javase.collections.comparator;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import org.junit.Test;public class ComparatorTest { @Testpublic void test() { List<Student> stus = new ArrayList<Student>(); Student zhangsan = new Student("zhangsan", 100); Student lisi = new Student("lisi", 90); Student wanger = new Student("wanger", 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); System.out.println("排序前");for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } System.out.println("-----------------------"); Collections.sort(stus, new Comparator<Student>() { @Overridepublic int compare(Student stu01, Student stu02) {// return stu01.getScore() - stu02.getScore();//升序return stu02.getScore() - stu01.getScore();// 降序 } }); System.out.println("排序后");for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } } }
In the compare(Student stu01, Student stu02) method, the attribute score is used as the sorting indicator, using "stu01.score-stu02.score", and the final results are arranged in ascending order. Vice versa in descending order.
Output:
##
The above is the detailed content of Comparison and use of Comparable and Comparator. For more information, please follow other related articles on the PHP Chinese website!