在java中對LIst集合的兩種排序方法(即sort的使用方法)
阿新 • • 發佈:2018-12-12
List集合的排序:
java提供了兩種排序方式,分別是Collections.sort(List)和Collections.sort(List,Commparator),下面就這兩種方法的使用做詳細的說明:
-Collections.sort(List);
sort的引數是一個List集合,對List集合中的資料排序,這個方法有兩種情況.
第一種情況是:如果List集合中的元素內容只有一個數據,就直接比較即可,前提是保證這個資料對應的類中有CompareTo方法
例如:(//)
package www.lxk.day15.commparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommparableClass {
/**
* 用Collections.sort(List)排序
* list元素的型別是String型別
* String 型別實現了Commparable介面,並重寫了CompareTo方法
* CompareTo方法中寫的是比較原則
*/
public void sortString(){
ArrayList<String> strs=new ArrayList<String>();
strs.add("123");
strs.add("987");
strs.add("abc");
strs.add("ABC");
System.out.println("---------------原始順序,沒有排序-------------");
for(String str:strs){
System.out.println(str);
}
System.out.println("----------------------------------------" );
Collections.sort(strs);
System.out.println("--------------- 經過排序後輸出 --------------");
for(String str:strs){
System.out.println(str);
}
}
}
如果List集合中的元素內容有多個數據,就需要這個元素型別必須實現Comparable介面,並重寫CompareTo方法,在此方法中指定排序原則
例如:
Student類中重寫了CompareTo方法,程式碼如下:
package www.lxk.day15.demo1;
import www.lxk.day15.demo1.CommonPerson;
public class Student extends CommonPerson implements Comparable<Student>{
private String stuNo;
public Student(){}
public Student(String name,int age,String stuNo) {
super(name,age);
this.stuNo = stuNo;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
@Override
public void study() {
System.out.println("Student.study()");
}
@Override
public void eat() {
System.out.println("Student.eat()");
}
public void exam(){
System.out.println("Student.exam()");
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
//按照年齡排序
int result1=this.getAge()-o.getAge();
return result1;
}
}
排序的程式碼如下:
package www.lxk.day15.commparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommparableClass {
/**
* 用Collections.sort(List)排序
* list元素的型別是Student型別
* String 型別實現了Commparable介面,並重寫了CompareTo方法
* CompareTo方法中寫的是比較原則
*/
public void sortStudent(){
ArrayList<Student> stus=new ArrayList<Student>();
Student stu1=new Student("張三",20,"S001");
Student stu2=new Student("李四",21,"S002");
Student stu3=new Student("王五",22,"S003");
Student stu4=new Student("張四",22,"S004");
stus.add(0,stu1);
stus.add(1,stu2);
stus.add(2,stu3);
stus.add(3,stu4);
System.out.println("---------------原始順序,沒有排序-------------");
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
System.out.println("----------------------------------------");
Collections.sort(stus);
System.out.println("--------------- 經過排序後輸出 --------------");
for(Student str:stus){
System.out.println("name="+str.getName()+"age="+str.getAge()+"stuNo="+str.getStuNo());
}
}
}
-Collections.sort(List,Commparator);
sort方法的引數有兩個,一個是要排序的List集合,另一個引數是Comparator介面,在比較器中,指定要排序的原則,
使用比較器方式就不用對要比較的集合的類型別實現Comparable介面
可以實現多個比較器,每個比較器就是一種排序原則
例如:
package www.lxk.day15.commparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import www.lxk.day15.demo1.Student;
public class ComparatorClass {
/**
* 此方法用於獲取一個List集合
* @return
*/
public List<Student> getStudents(){
List<Student> stus=new ArrayList<Student>();
Student stu1=new Student("張三",20,"S001");
Student stu2=new Student("李四",21,"S002");
Student stu3=new Student("王五",22,"S003");
Student stu4=new Student("張四",22,"S004");
stus.add(0,stu1);
stus.add(1,stu2);
stus.add(2,stu3);
stus.add(3,stu4);
return stus;
}
/**
* 根據Comparator介面的子實現來指定排序的原則,策略模式
* 按照姓名排序
* @param stus
*/
public void sortName(List<Student> stus){
Collections.sort(stus, new Comparator<Student>(){
@Override
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
return stu1.getName().compareTo(stu2.getName());
}
});
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
}
/**
* 根據Comparator介面的子實現來指定排序的原則,策略模式
* 按照年齡排序
* @param stus
*/
public void sortAge(List<Student> stus){
Collections.sort(stus, new Comparator<Student>(){
@Override
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
return stu1.getAge()-stu2.getAge();
}
});
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
}
/**
* 根據Comparator介面的子實現來指定排序的原則,策略模式
* 按照年齡排序
* @param stus
*/
public void sortstuNo(List<Student> stus){
Collections.sort(stus, new Comparator<Student>(){
@Override
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
return stu1.getStuNo().compareTo(stu2.getStuNo());
}
});
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
}
}
這其中還涉及到了策略設計模式,不過這並不是我們現在討論的重點,就不做詳細描述了.
**總結:如果就是一種比較原則,就用Commparable介面
如果有多個比較原則就用Comparator介面**