默認的排序方法:
讓類繼承Comparable接口,重寫compareTo方法。
示例代碼:java
package com.imooc.collection; import java.util.HashSet; import java.util.Objects; import java.util.Set; /** * 學生類 * Set中的元素是惟一的,不會重複,可是沒有順序。 */ public class Student implements Comparable<Student>{ private String id; private String name; // set集合只能使用 foreach 或 iterator進行遍歷,不能使用get()來獲取元素 public Set <Course> course; public Student(){ } public Student(String id, String name){ this.id = id; this.name = name; this.course = new HashSet<>(); } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setCourse(Set<Course> course) { this.course = course; } public String getId() { return id; } public String getName() { return name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name); } public Set getCourse() { return this.course; } @Override public int compareTo(Student o) { // 對ID進行排序 return this.id.compareTo(o.id); } }
臨時的排序方法:
Collection類自身有一個sort方法,須要傳入一個 Comparator 類,並重寫它的compare方法。
示例代碼:數組
package com.imooc.collection; import java.util.*; public class SetTest { private final List <Course> coursesToSelect = new ArrayList<>(); private final Scanner scanner = new Scanner(System.in); private Student student; public SetTest(){ } // 用於往courseToSelect中添加備選課程 public void testAdd(){ // 建立一個課程對象,並經過調用add方法,添加到備選課程List中 Course cr1 = new Course("1", "數據結構"); coursesToSelect.add(cr1); Course cr2 = new Course("2", "C語言"); coursesToSelect.add(0, cr2); // Course數組 Course[] course = {new Course("3", "離散數學"), new Course("4", "彙編語言")}; coursesToSelect.addAll(Arrays.asList(course)); Course[] course2 = {new Course("5", "高等數學"), new Course("6", "大學英語")}; coursesToSelect.addAll(2, Arrays.asList(course2)); } /** * 經過 foreach 方法來遍歷List */ public void testForeach(){ // System.out.println("(foreach)有以下課程待選:"); for (Object obj: coursesToSelect) { Course cr = (Course) obj; System.out.println("課程:" + cr.getId() + ":" + cr.getName()); } } /** * 遍歷Student集合中的全部元素 * @param student */ public void testForeachSet(Student student){ // 打印學生所選課程 for(Course cr: student.course) { System.out.println("選擇了課程:" + cr.getId() + ":" + cr.getName()); } } /** * 測試List的 contains 方法 * @param */ public void testListContainers(){ // 獲取備選課程序列的第0個元素 Course course = coursesToSelect.get(0); // 打印輸出coursesToSelected是否包含course對象 System.out.println("取得課程:" + course.getName()); System.out.println("備選課程中是否包含課程:" + course.getName() + "," + coursesToSelect.contains(course)); // 提示輸入課程名稱 System.out.println("請輸入課程名稱:"); String name = scanner.next(); Course course2 = new Course(); course2.setName(name); // 建立一個新的課程對象, ID和名稱 與course對象徹底同樣 // Course course2 = new Course(course.getId(), course.getName()); System.out.println("新建立課程對象:" + course2.getName()); System.out.println("備選課程中是否包含課程:" + course2.getName() + ","+ coursesToSelect.contains(course2)); // 經過indexOf()方法來獲取某元素的索引位置 if(coursesToSelect.contains(course2)){ System.out.println("課程:" + course2.getName() + "的索引位置爲:" + coursesToSelect.indexOf(course2)); } coursesToSelect.sort(new Comparator<Course>() {
// 重寫compare方法 @Override public int compare(Course o1, Course o2) { if(Integer.parseInt(o1.getId()) > Integer.parseInt(o2.getId())){ return 0; } return -1; } }); for(Course cr: coursesToSelect){ System.out.println("課程ID:" + cr.getId() + "課程名稱:" + cr.getName()); } }public static void main(String args[]){ SetTest st = new SetTest(); st.testAdd(); st.testForeach(); st.testListContainers(); } }