本問介紹了Collections工具類兩種sort()方法,具體如下:
一、Collections工具類兩種sort()方法
格式一: public static <T extends Comparable<? super T>> void sort(List<T> list)
說明:該方法中的泛型<T>都是Comparable介面的子類,即只有是Comparable介面子類類型的資料,才能進行比較排序。如果其他類型的資料要進行比較排序,必須繼承Comparable介面並
覆寫equals()和compareTo()方法。其中如String類、Integer類都是Comparable介面子類,可以進行排序,而基本類型不能進行sort排序。比較項目在類內指定
格式二:public static <T> void sort(List<T> list, Comparator<? super T> c)
說明:該方法中指定比較方式Comparator<? super T> c,即c必須實現Comparator<? super T>介面,覆寫compareTo()方法指定比較項目。比較項目在類外指定,比較靈活
二、樣本
樣本中擷取字串和數位公用方法:
/** * 產生隨機 不重複的字串 : number 產生字串個數 */ public static List<String> generateString(int number) { List<String> listString = new ArrayList<>(); // 用於存放傳回值 List<Integer> length = null; // 字串長度 StringBuffer sb = new StringBuffer(); // 中間變數 int control = 0; // 控制個數 String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; while (true) { // 控制結束 if ( control==number ) { break; } // 產生隨機數,產生36位的2aaab761-4341-4968-aceb-3861ee3824b2 UUID類型資料 String uuid = UUID.randomUUID().toString().replace("-", ""); sb.setLength(0); // 獲得隨機字串長度,長度不為0 do { length = getDiffNo(1, 11); } while ( length.get(0)==0 ); // 拼湊字串 for (int i=0; i<length.get(0); i++) { String str = uuid.substring(i*3, (i*3+3)); //將str字串轉換為16進位,獲得其值 int x = Integer.parseInt(str, 16); //取餘:x % 0x3E--0x3E = 3*16 + 14 = 62, 其中chars有62個字元 sb.append(chars[x % 0x3E]); } listString.add(sb.toString()); control++; } return listString; } /** * 產生隨機不重複的數字 :n產生個數 max產生範圍 */ public static List<Integer> getDiffNo(int n, int max) { // 產生 [0-n] 個不重複的隨機數 // list 用來儲存這些隨機數 List<Integer> list = new ArrayList<>(); Random random = new Random(); Integer k; for (int i=0; i<n; i++) { do { k = random.nextInt(max); } while (list.contains(k)); list.add(k); } return list; }
1、對Integer泛型的List進行排序
/** * 1.通過Collections.sort()方法,對Integer泛型的List進行排序; * 建立一個Integer泛型的List,插入十個100以內的不重複隨機整數, 調用Collections.sort()方法對其進行排序 * 2.定序:先數字後字母,數字0-9,字母A-Z a-z的順序 */ public void listIntegerSort() { // 插入十個100以內的不重複隨機整數 List<Integer> integerList = getDiffNo(10, 100); System.out.println("-------------排序前--------------"); for (Integer integer : integerList) { System.out.println("元素:" + integer); } Collections.sort(integerList); System.out.println("----------------排序後-------------------"); for (Integer integer : integerList) { System.out.println("元素:" + integer); } }
2、對String泛型的List進行排序
/** * 1.對String泛型的List進行排序; 建立String泛型的List,添加亂序的String元素, * 調用sort方法,再次輸出排序後的順序 */ public void listStringSort() { List<String> stringList = new ArrayList<String>(); stringList.add("eipJlcx"); stringList.add("WvQRufC"); stringList.add("J"); stringList.add("HdaU2G"); stringList.add("M0WswHD3"); System.out.println("------------排序前-------------"); for (String string : stringList) { System.out.println("元素:" + string); } Collections.sort(stringList); System.out.println("--------------排序後---------------"); for (String string : stringList) { System.out.println("元素:" + string); } }
/** * 對String泛型的List進行排序,要求隨機產生10個的不重複字串,字串的長度在10以內 */ public void listStringRandomSort() { // 產生隨機字串 List<String> listString = generateString(10); System.out.println("--------------排序前---------------"); for (String integer : listString) { System.out.println("元素:" + integer); } // 排序 Collections.sort(listString); System.out.println("----------------排序後------------------"); for (String integer : listString) { System.out.println("元素:" + integer); } }
3、對其他類型泛型的List進行排序
Course類實現
/** * 課程類 * @author Administrator * */public class Course { public String id; public String name; public Course(String id, String name) { this.id = id ; this.name = name; } public Course() { } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Course)) return false; Course other = (Course) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }}
Student類實現Comparable介面,類內設定比較項
import java.util.HashSet;import java.util.Set;/** * 學生類 * @author Administrator * */public class Student implements Comparable<Student> { public String id; public String name; public Set<Course> courses; public Student(String id, String name) { this.id = id; this.name = name; this.courses = new HashSet<Course>(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Student)) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public int compareTo(Student o) { // 設定ID為比較項 // TODO Auto-generated method stub return this.id.compareTo(o.id); }}
實現Comparator介面,類外設定比較項
import java.util.Comparator;public class StudentComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return o1.name.compareTo(o2.name); }}
比較Student類
/** * 對其他類型泛型的List進行排序,以Student為例。 */ public void listComparatorSort() { List<Student> studentList = new ArrayList<Student>(); List<Integer> list = getDiffNo(4, 1000); studentList.add(new Student(list.get(0) + "", "Mike")); studentList.add(new Student(list.get(1) + "", "Angela")); studentList.add(new Student(list.get(2) + "", "Lucy")); studentList.add(new Student(1000 + "", "Beyonce")); System.out.println("--------------排序前---------------"); for (Student student : studentList) { System.out.println("學生:" + student.id + ":" + student.name); } // 實現Comparator<T>介面,設定ID比較方式 Collections.sort(studentList); System.out.println("----------------按照ID排序後------------------"); for (Student student : studentList) { System.out.println("學生:" + student.id + ":" + student.name); } // 實現Comparator<T>介面,設定特定比較方式,以name比較排序 Collections.sort(studentList, new StudentComparator()); System.out.println("----------------按照姓名排序後-----------------"); for (Student student : studentList) { System.out.println("學生:" + student.id + ":" + student.name); } }
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。