一、Sort排序方法的使用:
1.对Integer类型进行排序 (该类型已经实现Comparable接口)
2.对String类类型进行排序 (该类型已经实现Comparable接口)
3.对其他类型进行排序 (需要自己实现Comparable接口)
二、Comparable接口和Comparator接口的比较:
1.Comparable接口
*实现类需要实现compareTo()方法
*实现该接口表示实例可以比较大小,可以进行自然排序
*是默认的比较规则
*compareTo()方法返回正数表示大,返回负数表示小,0表示相等
2.Comparator接口
*实现类需要实现Compare()方法
*用于定义临时的比较规则
3.Comparable和Comparator都是Java集合框架成员
三、比较规则
字符串类型:先数字,后大写字母,最后小写字母
整型:直接比较大小
四:案例演示:
1.Collections工具类的Sort()方法对Integer类型排序:
package com.bluesky;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsSort {
public void testSort(){
List<Integer> intList = new ArrayList<Integer>();
Random r = new Random();
Integer k;
for(int i=0;i<10;i++)
{
do{
k=r.nextInt(99)+1;
}while(intList.contains(k));
intList.add(k);
System.out.println("成功添加整数:"+k);
}
System.out.println("----------排序前-----------");
for(Integer i :intList){
System.out.print(i+" ");
}
System.out.println();
Collections.sort(intList);
System.out.println("----------排序后-----------");
for(Integer i :intList){
System.out.print(i+" ");
}
}
public static void main(String[] args) {
CollectionsSort cs = new CollectionsSort();
cs.testSort();
}
}
运行结果:
2.对String类型排序:
package com.bluesky;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsSort {
public void testSort(){
public void testSort2(){
List<String> strList = new ArrayList<String>();
strList.add("Lenover");
strList.add("liu");
strList.add("microsoft");
strList.add("boss");
System.out.println("----------排序前-----------");
for(String str:strList){
System.out.println(str);
}
Collections.sort(strList);
System.out.println("----------排序后-----------");
for(String str:strList){
System.out.println(str);
}
}
public static void main(String[] args) {
CollectionsSort cs = new CollectionsSort();
cs.testSort2();
}
}
运行结果:
因为是字符串类型,数字最大,然后是大写字母,最后小写字母;
3.对其他类型进行排序:
Student类:注意要实现Comparable接口以及compareTo()方法
package com.bluesky;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
public class Student implements Comparable<Student> {
public String id;
public String name;
public Set<Course> set;
public Student(){
}
public Student(String id,String name)
{
this.id=id;
this.name=name;
this.set=new HashSet();
}
public int compareTo(Student arg0) {
return this.id.compareTo(arg0.id);
}
}
测试类:
运行结果:
这里的ID我们定义为String类型,所以1比2小,14在第一位;该种情况只比较第一位;
根据姓名排序,这是需要定义临时比较规则;
新建StuCompartor类实现Comparator接口已经compare()方法:
package com.bluesky;
import java.util.Comparator;
public class StuComparator implements Comparator<Student>{
@Override
public int compare(Student stu1, Student stu2) {
return stu1.name.compareTo(stu2.name);
}
}
使用新的比较规则并排序: Collections.Sort(stuList,new StuComparator());
package com.bluesky;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsSort {
public void testSort3(){
List<Student> stuList = new ArrayList<Student>();
stuList.add(new Student("14","Lenover"));
stuList.add(new Student("4","Kiu"));
stuList.add(new Student("3","Microsoft"));
stuList.add(new Student("2","Boss"));
System.out.println("----------排序前-----------");
for(Student stu:stuList)
{
System.out.println("ID:"+stu.id+"name:"+stu.name);
}
Collections.sort(stuList,new StuComparator());
System.out.println("----------排序后-----------");
for(Student stu:stuList)
{
System.out.println("ID:"+stu.id+" "+"name:"+stu.name);
}
}
public static void main(String[] args) {
CollectionsSort cs = new CollectionsSort();
cs.testSort3();
}
}
运行结果:
本文详细介绍了Java中Collections的Sort方法如何对List进行排序,包括Integer和String类型的排序,并讨论了Comparable和Comparator接口的区别。在Comparable接口中,实现compareTo()方法以进行自然排序;而Comparator接口则通过实现compare()方法定义临时比较规则。文章还提供了具体的案例,如对自定义Student类的排序,展示了如何依据不同属性进行排序。
1933

被折叠的 条评论
为什么被折叠?



