【JavaSE筆記】集合(四)_TreeSet兩種排序

本期知識點:
再談TreeSet

保證元素排序的兩種方式:
自然排序
比較器排序

1.自然排序:讓元素所屬的類實現 Comparable接口


TreeSet<Student> a = new TreeSet<Student>();   空參的構造就是默認的自然排序

class Student implements Comparable<Student>{ }   元素所屬的類必須實現一個自然排序的接口

接口 Comparable<T>:

public interface Comparable<T>此接口強制實行它的每個類的對象進行整體排序。這種排序被稱爲類的自然排序。類的 compareTo 方法被稱爲它的自然比較方法。

a.方法:
int compareTo(T o) 比較此對象與指定對象的順序。如果該對象小於、等於或大於指定對象,則分別返回負整數、零或正整數。

class Student implements Comparable<Student>{
	private String name;
	private int age;
	
	public Student() {
		super();
	}
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	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;
	}
	@Override
	public String toString() {
		return name+"---"+age;
	}
	@Override
	public int compareTo(Student o) {
		
		int a = this.getName().compareTo(o.getName());
		return a;
	}
	
}

import java.util.TreeSet;

public class Demo01 {
	public static void main(String[] args) {
		TreeSet<Student> a = new TreeSet<Student>();
		
		a.add(new Student("Nick",23));
		a.add(new Student("Ash",23));
		a.add(new Student("Tom",21));
		a.add(new Student("Peter",20));
		a.add(new Student("Jack",21));
		a.add(new Student("Lin",22));
		
		for (Student s :a) {
			System.out.println(s);
		}
	}
}


2.比較器排序:讓集合構造方法接收 Comparator的實現對象。

public MyC implements Comparator<Student>{ }   自定義一個類(MyC),該類就是Comparator接口的子實現類


接口 Comparator<T>:
public interface Comparator<T>強行對某個對象 collection 進行整體排序的比較函數。
a.方法:
int compare(T o1, T o2) 比較用來排序的兩個參數。根據第一個參數小於、等於或大於第二個參數分別返回負整數、零或正整數。

boolean equals(Object obj)指示某個其他對象是否“等於”此 Comparator。


b.在開發中常用匿名內部類的方式,開發效率高。
TreeSet<Student> t = new TreeSet<Student>(new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
......
}

});

class Student {
	private String name;
	private int age;
	
	public Student() {
		super();
	}
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	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;
	}
	@Override
	public String toString() {
		return name+"---"+age;
	}
}

public class MyC implements Comparator<Student> {

	@Override
	public int compare(Student o1, Student o2) {
		//比較名字長度
		int a = o1.getName().length()-o2.getName().length();
		//再比較名字按字典順序
		int b = a == 0 ? o1.getName().compareTo(o2.getName()):a;
		//再比較年齡
		int c = b == 0 ? o1.getAge()-o2.getAge():b;
		return c;
	}
}

import java.util.TreeSet;

public class Demo01 {
	public static void main(String[] args) {
		TreeSet<Student> a = new TreeSet<Student>(new MyC());
		
		a.add(new Student("aaabbbe", 27));
		a.add(new Student("aaa", 27));
		a.add(new Student("aaabbbccd", 38));
		a.add(new Student("aaabbbc", 27));
		a.add(new Student("aaabbbcccdde", 27));
		a.add(new Student("aaabbbccc", 38));
		a.add(new Student("aab", 29));
		a.add(new Student("aaabbbcccddd", 26));
		
	
		for (Student s : a) {
			System.out.println(s);
		}
	}
}


發佈了51 篇原創文章 · 獲贊 15 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章