不废话了 , 直接给大家贴代码了。
istilah kelas {string str; int id; istilah publik (string str, int id) {this.str = str; this.id = id; } public string toString () {return str+""+id; }} class STERM mengimplementasikan sebanding {string str; int id; sterm publik (string str, int id) {this.str = str; this.id = id; } compareto int publik (objek o) {return ((sterm) o) .id - id; } public string toString () {return str+""+id; }} // Method1: Eksplisit mengimplementasikan comparator class termompeparator mengimplementasikan pembanding {public int perbandingan (objek o1, objek o2) {return ((term) o1) .id - ((istilah) o2) .id; }} Public Class T1 { / ** * @param args * / public static void main (string [] args) {// TODO Metode yang dihasilkan secara otomatis stub // arrayList <Integer> arr = ArrayList baru <Integer> (arrays. aslist (3,1,3,7,8,0)); // // collections.sort (arr, comparator baru () {// // public int perbandingan (objek o1, objek o2) {// return new double ((integer) o1) .compareto (double baru ((integer) o2)); //} //}); // Daftar Method1 <TERM> ls = ArrayList baru <Term> (); ls.add (istilah baru ("a", 1)); ls.add (istilah baru ("b", 5)); ls.add (istilah baru ("c", 2)); ls.add (istilah baru ("d", 2)); ls.add (istilah baru ("e", 3)); ls.add (istilah baru ("f", 0)); Collections.sort (ls, termompeparator baru ()); System.out.println (ls); // [f 0, a 1, c 2, d 2, e 3, b 5] // method2: anonim mengimplementasikan collections.sort (ls, pembanding baru () {public int perbandingan (Objek O1, Objek O2) {return ((istilah) o2) .id - ((istilah) o1) .id; System.out.println (ls); // [B 5, E 3, C 2, D 2, A 1, F 0] // Method3: Instantiate a Comparator Template Comparator <TERM> termCMP = Komparator baru <TERIM> ( ) {public int perbandingan (istilah t1, istilah t2) {return t1.id - t2.id; }}; Collections.sort (ls, termcmp); System.out.println (ls); // [f 0, a 1, c 2, d 2, e 3, b 5] // method4: elemen mengimplementasikan daftar yang sebanding <serm> lss = new ArrayList <STERM> () ; lss.add (sterm baru ("A", 1)); lss.add (sterm baru ("b", 5)); lss.add (sterm baru ("c", 2)); lss.add (sterm baru ("d", 2)); lss.add (sterm baru ("e", 3)); lss.add (sterm baru ("f", 0)); Collections.sort (LSS); System.out.println (LSS); // [B 5, E 3, C 2, D 2, A 1, F 0]}}Prioriyqueue 的用法和上述的排序类似 , : :
kelas WordFreq mengimplementasikan {public string wd; Publik int freq; wordfreq publik (string wd, int freq) {this.wd = wd; this.freq = freq; } public int compareto (objek o) {return ((wordfreq) o) .freq - freq; } public string toString () {return wd+""+freq; }} public class testt {public static void main (string [] args) {// TODO Metode yang dihasilkan secara otomatis Stub priityqueuee <WordFreq> pq = new PriityQueue <WordFreq> (); pq.offer (wordfreq baru ("aaa", 3)); pq.offer (wordfreq baru ("bbb", 4)); pq.offer (wordfreq baru ("ccc", 1)); while (pq.peek ()! = null) {System.out.println (pq.poll ()); } // 从大到小输出}}注意 ,
untuk (wordfreq wf: pq) {system.out.println (wf);}并不保证遍历的有序
如果 Daftar <String> ls 进行排序的话 , 不需要写 pembanding, 因为 String 本身有 compareto 的实现。