天天看點

Collections&Comparator&Comparable類API使用Comparator排序使用Comparable排序

//	向集合中添加元素,可以是泛型的集合形式的,也可以是T的可變參數(多個T類型資料,或者T類型數組)
public static <T> boolean AddAll(Collection<T> c, T... elements)

//	将清單亂序
public static shuffle(List<?> list)

//	将清單按照預設規則排序
public static <T> void sort(List<T> list)

//	将清單按照給定規則排序
public static <T> void sort(List<T> list, Comparator<? super T>)
           
//	Comparator排序需要實作的方法,o1-o2是升序;o2-o1是降序
public int compare(String o1, String o2)
           

使用Comparator排序

//	一個對象
public class Student() {
	private String name;
	private int age;
	
	public Studnet() {
	}
	
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public int getAge() {
		return age;
	}
	
	@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
//	測試
//	o1 - o2升序;o2 - o1降序
public void Demo() {
	public static void main(String[] args) {
		List students = new ArrayList<String>();
		students.add(new Student("StudentA", 10));
		students.add(new Student("StudentD", 40));
		students.add(new Student("StudentC", 30));
		students.add(new Student("StudentE", 50));
		students.add(new Student("StudentB", 20));
		Collections.sort(students, Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o2.getAge() - o1.getAge();
			}
		});
		for(Student student : students) {
			System.out.println(student);
		}
	}
}
//	Student{name='StudentE', age=50}
//	Student{name='StudentD', age=40}
//	Student{name='StudentC', age=30}
//	Student{name='StudentB', age=20}
//	Student{name='StudentA', age=10}
           

使用Comparable排序

//	一個對象
public class Student() implements Comparable<Student>{
	private String name;
	private int age;
	
	public Studnet() {
	}
	
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public int getAge() {
		return age;
	}
	
	@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

	@Override
	public int compareTo(String o) {
		//	this-o是升序
		//	o-this是降序
		return this.age - o.age;
	}
}
//	測試
public void Demo() {
	public static void main(String[] args) {
		List students = new ArrayList<String>();
		students.add(new Student("StudentA", 10));
		students.add(new Student("StudentD", 40));
		students.add(new Student("StudentC", 30));
		students.add(new Student("StudentE", 50));
		students.add(new Student("StudentB", 20));
		Collections.sort(students);
		for(Student student : students) {
			System.out.println(student);
		}
	}
}
//	Student{name='StudentE', age=50}
//	Student{name='StudentD', age=40}
//	Student{name='StudentC', age=30}
//	Student{name='StudentB', age=20}
//	Student{name='StudentA', age=10}