List集合的排序:
java提供了兩種排序方式,分別是Collections.sort(List)和Collections.sort(List,Commparator),下面就這兩種方法的使用做詳細(xì)的說明:
方法一:Collections.sort(List)
這個方法有分兩種情況:1、比較的是基礎(chǔ)數(shù)據(jù) 2、比較的是引用數(shù)據(jù)
1、基礎(chǔ)數(shù)據(jù)的比較呢,一般都是直接比較,因?yàn)榛A(chǔ)數(shù)據(jù)都實(shí)現(xiàn)了CompareTo()方法,例如
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommparableClass {
/**
* 用Collections.sort(List)排序
* list元素的類型是String類型
* String 類型實(shí)現(xiàn)了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("--------------- 經(jīng)過排序后輸出 --------------");
for(String str:strs){
System.out.println(str);
}
}
}
比較的是引用數(shù)據(jù),引用數(shù)據(jù)就必須要我們實(shí)現(xiàn)Comparable接口,并重寫CompareTo方法,在此方法中指定排序原則,例如
public class Student implements Comparable<Student>{
public String stuNo;
public String name;
public Integer age;
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
//按照年齡排序
int result1=this.getAge()-o.getAge();
return result1;
}
}
排序的情況如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommparableClass {
/**
* 用Collections.sort(List)排序
* list元素的類型是Student類型
* String 類型實(shí)現(xiàn)了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("--------------- 經(jīng)過排序后輸出 --------------");
for(Student str:stus){
System.out.println("name="+str.getName()+"age="+str.getAge()+"stuNo="+str.getStuNo());
}
}
}
方法二:Collections.sort(List,Commparator)
-Collections.sort(List,Commparator);
sort方法的參數(shù)有兩個,一個是要排序的List集合,另一個參數(shù)是Comparator接口,在比較器中,指定要排序的原則,
使用比較器方式就不用對要比較的集合的類類型實(shí)現(xiàn)Comparable接口文章來源:http://www.zghlxwxcb.cn/news/detail-700906.html
可以實(shí)現(xiàn)多個比較器,每個比較器就是一種排序原則文章來源地址http://www.zghlxwxcb.cn/news/detail-700906.html
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;
}
/**
* 根據(jù)Comparator接口的子實(shí)現(xiàn)來指定排序的原則,策略模式
* 按照姓名排序
* @param stus
*/
public void sortName(List<Student> stus){
Collections.sort(stus, new Comparator<Student>(){
@Override
public int compare(Student stu1, Student stu2) {
return stu1.getName().compareTo(stu2.getName());
}
});
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
}
/**
* 根據(jù)Comparator接口的子實(shí)現(xiàn)來指定排序的原則,策略模式
* 按照年齡排序
* @param stus
*/
public void sortAge(List<Student> stus){
Collections.sort(stus, new Comparator<Student>(){
@Override
public int compare(Student stu1, Student stu2) {
return stu1.getAge()-stu2.getAge();
}
});
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
}
/**
* 根據(jù)Comparator接口的子實(shí)現(xiàn)來指定排序的原則,策略模式
* 按照年齡排序
* @param stus
*/
public void sortstuNo(List<Student> stus){
Collections.sort(stus, new Comparator<Student>(){
@Override
public int compare(Student stu1, Student stu2) {
return stu1.getStuNo().compareTo(stu2.getStuNo());
}
});
for(Student stu:stus){
System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
}
}
}
隨機(jī)排序
Collections.shuffle(list); //進(jìn)行隨機(jī)排序
到了這里,關(guān)于list集合對sort的使用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!