TreeSet 覆蓋避免

import java.util.Comparator;
import java.util.TreeSet;
/**
 * !!!使用TreeSet排序用於比較的變量如果相等會被覆蓋
 * 定義的compare和compareTo方法應避免返回零值,
 * 可增加唯一值如id的compare來避免覆蓋
 * @author 風瀟瀟
 *
 */
public class StudentApp {
	
	
	public static void main(String[] args) {
		TreeSet<Student> students = new TreeSet<Student>(new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getScore()<o2.getScore() ?1:(o1.getScore()==o2.getScore()?o1.getName().compareTo(o2.getName()):-1);
			}
		});
		Student s1 = new Student("wx",1,100);
		Student s2 = new Student("hx",3,90);
		Student s3 = new Student("sx",2,90);
		students.add(s1);
		students.add(s2);
		students.add(s3);
		System.out.println(students);
		System.out.println("=================");
		TreeSet<Student> students1 = new TreeSet<Student>();
		students1.add(s1);
		students1.add(s2);
		students1.add(s3);
		System.out.println(students1);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章