Collections工具类与Comparator比较器

1.Collections类提供了对集合操作的静态方法(常用)
(1)void sort(List list)//对列表list按元素自然顺序升序排列,元素对应类型必须实现Comparable接口
//定义类时实现Comparable接口,表明该类对象可比较大小
(2)void reverse(List list)//反转列表list中的所有元素
(3)void sort(List list,Comparator c)//对列表list中的所有元素按比较器c升序排列
(4)int binarySearch(List list,Object o)//使用折半查找法在列表list中查找与o“相等”的元素,返回找到元素的索引,列表a必须有序
(5)void shuffle(List list)//将列表list中的所有元素随机打乱
(6)int lastIndexOfSubList(List src,List target)//得到子列表target在列表src中最后一次出现的索引
(7)int indexOfSubList(List src,List target)//得到子列表target在列表src中第一次出现的索引
2.
在这里插入图片描述

Example 1------基本类型的包装类排序

import java.util.ArrayList;
import java.util.Collections;


public class Main4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
    ArrayList<Integer> lis1=new ArrayList<Integer>();
    lis1.add(3);
    lis1.add(2);
    lis1.add(1);
    lis1.add(6);
    System.out.println("原集合输出为"+lis1);
    Collections.sort(lis1);
    System.out.println("排序后的集合输出为"+lis1);
	}

}

Example 2-----Collections类的使用,学生的成绩按从大到小(降序)排列

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class main5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Student> stulist = new ArrayList<Student>();
		// 创建学生对象
		Student stu1 = new Student("001", "Lisa", 98);
		Student stu2 = new Student("002", "Jim", 97);
		Student stu3 = new Student("666", "Talor", 100);
		Student stu4 = new Student("222", "Tom", 99);
		// 往stulist的容器里面添加各个学生对象
		stulist.add(stu1);
		stulist.add(stu2);
		stulist.add(stu3);
		stulist.add(stu4);
		Collections.sort(stulist, new Comparator<Student>() {

			// 自定义排序规则
			public int compare(Student s1, Student s2) {
				return s2.getScore() - s1.getScore();// 按照成績的降序排列

			}
		});

		for (Student s : stulist) {
			System.out.println(s);// 将排序完的Student对象s进行遍历输出
		}

	}
}

class Student {
	String id, name;
	int score;

	public Student() {
		super();
	}

	public Student(String id, String name, int score) {
		super();
		this.id = id;
		this.name = name;
		this.score = score;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", score=" + score
				+ "]";
	}

}

运行结果如下
在这里插入图片描述
Example—3
自定义排序的几种写法
(1)自定义类实现Comparable接口定义对象的排序规则
自定义类写成匿名类的形式

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Student> stulist = new ArrayList<Student>();
		// 创建学生对象
		Student stu1 = new Student("001", "Lisa", 98);
		Student stu2 = new Student("002", "Jim", 97);
		Student stu3 = new Student("666", "Talor", 100);
		Student stu4 = new Student("222", "Tom", 99);
		// 往stulist的容器里面添加各个学生对象
		stulist.add(stu1);
		stulist.add(stu2);
		stulist.add(stu3);
		stulist.add(stu4);
		Collections.sort(stulist, new Comparator1());//创建接口对象
		for (Student s : stulist) {
			System.out.println(s);// 将排序完的Student对象s进行遍历输出
		}
	}

}

class Comparator1 implements Comparator<Student> {

	@Override
	public int compare(Student s1, Student s2) {
		// TODO Auto-generated method stub
		return s2.getScore() - s1.getScore();
		// 自定义比较器,用于设置排序规则 (降序排列)
	}

}

class Student {
	String num;// 学号
	String name;// 名字
	int score;

	public Student(String num, String name, int score) {
		super();
		this.num = num;
		this.name = name;
		this.score = score;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getName() {
		return name;
	}

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

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [num=" + num + ", name=" + name + ", score=" + score
				+ "]";
	}

}

在这里插入图片描述
(2)JDK8才支持的lambda表达式,适用于排序规则比较简单
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main7 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	List<Student> stulist = new ArrayList<Student>();
	// 创建学生对象
	Student stu1 = new Student("001", "Lisa", 98);
	Student stu2 = new Student("002", "Jim", 97);
	Student stu3 = new Student("666", "Talor", 100);
	Student stu4 = new Student("222", "Tom", 99);
	// 往stulist的容器里面添加各个学生对象
	stulist.add(stu1);
	stulist.add(stu2);
	stulist.add(stu3);
	stulist.add(stu4);
	Collections.sort(stulist,(s1,s2)->s2.getScore()-s1.getScore());
	//lambda表达式的格式Collections.sort(  该列表即该容器,两个对象的比较规则)
	for (Student s : stulist) {
		System.out.println(s);// 将排序完的Student对象s进行遍历输出
	}
}

}

class Student {
private String num;// 学号
private String name;// 名字
private int score;

public Student(String num, String name, int score) {
	super();
	this.num = num;
	this.name = name;
	this.score = score;
}

public String getNum() {
	return num;
}

public void setNum(String num) {
	this.num = num;
}

public String getName() {
	return name;
}

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

public int getScore() {
	return score;
}

public void setScore(int score) {
	this.score = score;
}

@Override
public String toString() {
	return "Student [num=" + num + ", name=" + name + ", score=" + score
			+ "]";
}

}
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值