面试小结:对List集合中的元素进行排序

一、简单数据的排序,如int,Integer类型的数据可以直接使用Collections中的sort()方法;

public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();
		list.add(7);
		list.add(1);
		list.add(6);
		list.add(3);
		list.add(6);
		list.add(8);
		//正常情况下,list中的元素顺序是元素的添加顺序
		System.out.println("默认顺序");
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));//打印出:7、1、6、3、6、8
		}
		System.out.println("排序后");
		//利用集合工具类的sort()方法实现排序
		Collections.sort(list);
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));//打印出:1、3、6、6、7、8
		}		
	}	

二、当list中的元素是对象时,要排序的对象需要实现comparable接口,通过重写compareTo()方法自定义排序方式,此处使用Student类

public class Student implements Comparable<Student>{
	
	private String name;
	
	private int age;
	
	private float score;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public float getScore() {
		return score;
	}

	public void setScore(float score) {
		this.score = score;
	}
	
	public Student(String name, int age, float score) {
		super();
		this.name = name;
		this.age = age;
		this.score = score;
	}

	@Override
	public int compareTo(Student o) {
		int i = this.age - o.age;
		if(i==0) {
			float j = this.score - o.score;
			return (int)j;
		}
		return i;
	}	
}
public static void main(String[] args) {
		//新建三个学生对象
		Student zhangsan = new Student("张三", 15, 90);
		Student lisi = new Student("李四", 30, 89);
		Student chenwu = new Student("陈五", 13, 100);
		List<Student> studentList = new ArrayList<>();
		studentList.add(zhangsan);
		studentList.add(lisi);
		studentList.add(chenwu);
		//调用集合工具类Collections的sort()方法,如果不实现Comparable接口,是无法调用此方法的
		Collections.sort(studentList);
		for(Student stu : studentList) {
			System.out.println("姓名:"+stu.getName()+"年龄:"+stu.getAge()+"分数:"+stu.getScore());
		}		
	}
打印结果:姓名:陈五 年龄:13 分数:100.0
                 姓名:张三 年龄:15 分数:90.0

                 姓名:李四 年龄:30 分数:89.0

三、还有一种方法不需要在类上实现comparable接口,可以直接使用以下方法:

public static void main(String[] args) {
		//新建三个学生对象
		Student zhangsan = new Student("张三", 15, 90);
		Student lisi = new Student("李四", 30, 89);
		Student chenwu = new Student("陈五", 13, 100);
		List<Student> studentList = new ArrayList<>();
		studentList.add(zhangsan);
		studentList.add(lisi);
		studentList.add(chenwu);
		//直接调用集合工具类的sort()
		Collections.sort(studentList, new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				int i = o1.getAge() - o2.getAge();
				if(i==0) {
					float j = o1.getScore() - o2.getAge();
					return (int)j;
				}
				return i;
			}		
		});
		
		for(Student stu : studentList) {
			System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge()+" 分数:"+stu.getScore());
		}	
	}
打印结果:姓名:陈五 年龄:13 分数:100.0
                 姓名:张三 年龄:15 分数:90.0

                 姓名:李四 年龄:30 分数:89.0

这种方法和第二种的效果是一样的,以上就是是Java中Colelctions工具类为我们提供的两种集合排序方法。

总结:list集合中如果是基本数据类型可以直接使用Collections.sort()进行排序,因为java已经帮我们实现了Comparable接口,

自定义的类可以通过实现Comparable接口进行排序,也可以直接调用Colelctions工具类的sort(),不过需要增加一个Comparable对象参数,并重写compare()方法。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值