Java中应用Collections工具类的Sort()方法对List进行排序

本文详细介绍了Java中Collections的Sort方法如何对List进行排序,包括Integer和String类型的排序,并讨论了Comparable和Comparator接口的区别。在Comparable接口中,实现compareTo()方法以进行自然排序;而Comparator接口则通过实现compare()方法定义临时比较规则。文章还提供了具体的案例,如对自定义Student类的排序,展示了如何依据不同属性进行排序。

一、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();
	}

}


运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值