- 一、操作Comparable接口
- 1.集合排序
Collections的sort()方法要求被排序的对象,必须操作java.lang.Comparable接口,这个接口有个compareTo()方法,该方法返回大于0,等于0或者小于0的数。Collections的sort()方法在取得a对象与b对象进行比较的时候,会先将a对象扮演为Comparable(若对象没有操作Comparable,将会抛出ClassCastException),调用a.compareTo(b),如果a对象顺序上小于b对象则返回小于0的值,若顺序上相等则返回0,若顺序上a大于b则返回大于0的值。
- 集合中存放的是字符串,字符串实现了Comparable接口,操作了CompareTo()方法
List list = new ArrayList<>(Arrays.asList("zhang","wang","li","sun"));
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list);
结果 [zhang, wang, li, sun]
[li, sun, wang, zhang]
- 集合中存放的是包装类Integer,包装类实现了Comparable接口,操作了CompareTo()方法
List list = new ArrayList<>(Arrays.asList(2,1,7,11,5,6));
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list);
结果 [2, 1, 7, 11, 5, 6]
[1, 2, 5, 6, 7, 11]
- 集合中存放的是普通类,则类要实现Comparable接口,操作CompareTo()方法
package com.dayuan.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortClass {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("1001", 90));
list.add(new Student("1002", 70));
list.add(new Student("1003", 100));
list.add(new Student("1004", 50));
System.out.println("原順序:" + list);
Collections.sort(list);
System.out.println("自定义顺序:" + list);
}
}
// 操作Comparable
class Student implements Comparable {
private String id;
private int score;
Student() {
}
Student(String id, int score) {
this.id = id;
this.score = score;
}
//a.compareTo(b) 为正序
//b.compareTo(a) 为倒序
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
@Override
public String toString() {
return String.format("Student(%s,%s)", id, score);
}
}
Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score)),为倒序
结果
原順序:[Student(1001,90), Student(1002,70), Student(1003,100), Student(1004,50)]
自定义顺序:[Student(1003,100), Student(1001,90), Student(1002,70), Student(1004,50)]
Integer.valueOf(this.score).compareTo(Integer.valueOf(stu.score)),为正序
结果
原順序:[Student(1001,90), Student(1002,70), Student(1003,100), Student(1004,50)]
自定义顺序:[Student(1004,50), Student(1002,70), Student(1001,90), Student(1003,100)]
或者Integer.valueOf(this.score).compareTo(Integer.valueOf(stu.score))*(-1)也是倒序。
- 2.数组排序
- 数组中存放的是字符串,字符串实现了Comparable接口,操作了CompareTo()方法
String[] str = new String[]{"china","guangzhou","beijing"};
System.out.println(Arrays.toString(str));
Arrays.sort(str);
System.out.println(Arrays.toString(str));
结果[china, guangzhou, beijing]
[beijing, china, guangzhou]
- 数组中存放的是包装类Integer,包装类实现了Comparable接口,操作了CompareTo()方法
Integer[] str = new Integer[]{1,2,4,3,7};
System.out.println(Arrays.toString(str));
Arrays.sort(str);
System.out.println(Arrays.toString(str));
结果[1, 2, 4, 3, 7]
[1, 2, 3, 4, 7]
- 数组中存放的是普通对象
package com.dayuan.test;
import java.util.Arrays;
class Student implements Comparable {
private String id;
private int score;
Student() {
}
Student(String id, int score) {
this.id = id;
this.score = score;
}
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}
public class SortClass {
public static void main(String[] args) {
Student[] stus = new Student[] { new Student("1001", 90), new Student("1002", 70), new Student("1003", 100),
new Student("1004", 50), };
Arrays.sort(stus);
}
}
- 二、操作Comparator接口
Comparator接口的compare方法,会传入两个对象,如果o1顺序上小于o2则返回小于0的值,顺序相等则为0,顺序上o1大于o2则返回大于0的值。
- 集合排序
- 集合中存放的是字符串
List list = new ArrayList();
list.add("你好!");
list.add("guangzhou");
list.add("china");
list.add("beijing");
System.out.println("原順序:"+list);
Collections.sort(list, new Comparator() {
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);//-1、0、1
}
});
System.out.println("自定義順序:"+list);
Collections.reverse(list);
System.out.println("然後再反序:"+list);
结果
原順序:[你好!, guangzhou, china, beijing]
自定義順序:[beijing, china, guangzhou, 你好!]
然後再反序:[你好!, guangzhou, china, beijing]
- 集合中存放的是普通对象
package com.dayuan.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortClass {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("1001", 90));
list.add(new Student("1002", 70));
list.add(new Student("1003", 100));
list.add(new Student("1004", 50));
System.out.println("原順序:" + list);
Collections.sort(list,new Comparator(){
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return Integer.valueOf(o1.getScore()).compareTo(o2.getScore());
}
});
System.out.println("自定义顺序:" + list);
}
}
// 操作Comparable
class Student {
private String id;
private int score;
Student() {
}
Student(String id, int score) {
this.id = id;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return String.format("Student(%s,%s)", id, score);
}
}
结果为
原順序:[Student(1001,90), Student(1002,70), Student(1003,100), Student(1004,50)]
自定义顺序:[Student(1004,50), Student(1002,70), Student(1001,90), Student(1003,100)]
- 2、数组排序
- 数组中存放的是普通对象
package com.dayuan.test;
import java.util.Arrays;
import java.util.Comparator;
class Student {
private String id;
private int score;
Student() {
}
Student(String id, int score) {
this.id = id;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
public class SortClass {
public static void main(String[] args) {
Student[] stus = new Student[] { new Student("1001", 90), new Student("1002", 70), new Student("1003", 100),
new Student("1004", 50), };
Arrays.sort(stus,new Comparator() {
@Override
public int compare(Student o1, Student o2) {
return Integer.valueOf(o1.getScore()).compareTo(Integer.valueOf(o2.getScore()));
}
});
}
}
在java中,与顺序有关的行为,通常要不对象本身是Comparable,要不就是另行指定Comparable对象告知如何排序。
- 三、compareTo方法原理
比较A、B连个int类型数字,A
if(A -1;
}else if(A==B){
return 0;
}else{
return 1;
}
改为三元运算符
AB?1:0)