java中Collections.sort排序詳解
Comparator是個(gè)接口,可重寫compare()及equals()這兩個(gè)方法,用于比價(jià)功能;如果是null的話,就是使用元素的默認(rèn)順序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g這樣,當(dāng)然數(shù)字也是這樣的。
compare(a,b)方法:根據(jù)第一個(gè)參數(shù)小于、等于或大于第二個(gè)參數(shù)分別返回負(fù)整數(shù)、零或正整數(shù)。
equals(obj)方法:僅當(dāng)指定的對(duì)象也是一個(gè) Comparator,并且強(qiáng)行實(shí)施與此 Comparator 相同的排序時(shí)才返回 true。
Collections.sort(list, new PriceComparator());的第二個(gè)參數(shù)返回一個(gè)int型的值,就相當(dāng)于一個(gè)標(biāo)志,告訴sort方法按什么順序來對(duì)list進(jìn)行排序。
具體實(shí)現(xiàn)代碼方法如下:
Book實(shí)體類:
package com.tjcyjd.comparator;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.TreeMap;
/**
* 書實(shí)體類
*
* @author yjd
*
*/
public class Book implements Comparable { // 定義名為Book的類,默認(rèn)繼承自O(shè)bject類
public int id;// 編號(hào)
public String name;// 名稱
public double price; // 價(jià)格
private String author;// 作者
public GregorianCalendar calendar;// 出版日期
public Book() {
this(0, "X", 0.0, new GregorianCalendar(), "");
}
public Book(int id, String name, double price, GregorianCalendar calender,
String author) {
this.id = id;
this.name = name;
this.price = price;
this.calendar = calender;
this.author = author;
}
// 重寫繼承自父類Object的方法,滿足Book類信息描述的要求
public String toString() {
String showStr = id + "\t" + name; // 定義顯示類信息的字符串
DecimalFormat formatPrice = new DecimalFormat("0.00");// 格式化價(jià)格到小數(shù)點(diǎn)后兩位
showStr += "\t" + formatPrice.format(price);// 格式化價(jià)格
showStr += "\t" + author;
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
showStr += "\t" + formatDate.format(calendar.getTime()); // 格式化時(shí)間
return showStr; // 返回類信息字符串
}
public int compareTo(Object obj) {// Comparable接口中的方法
Book b = (Book) obj;
return this.id - b.id; // 按書的id比較大小,用于默認(rèn)排序
}
public static void main(String[] args) {
Book b1 = new Book(10000, "紅樓夢(mèng)", 150.86, new GregorianCalendar(2009,
01, 25), "曹雪芹、高鄂");
Book b2 = new Book(10001, "三國演義", 99.68, new GregorianCalendar(2008, 7,
8), "羅貫中 ");
Book b3 = new Book(10002, "水滸傳", 100.8, new GregorianCalendar(2009, 6,
28), "施耐庵 ");
Book b4 = new Book(10003, "西游記", 120.8, new GregorianCalendar(2011, 6,
8), "吳承恩");
Book b5 = new Book(10004, "天龍八部", 10.4, new GregorianCalendar(2011, 9,
23), "搜狐");
TreeMap tm = new TreeMap();
tm.put(b1, new Integer(255));
tm.put(b2, new Integer(122));
tm.put(b3, new Integer(688));
tm.put(b4, new Integer(453));
tm.put(b5, new Integer(40));
Iterator it = tm.keySet().iterator();
Object key = null, value = null;
Book bb = null;
while (it.hasNext()) {
key = it.next();
bb = (Book) key;
value = tm.get(key);
System.out.println(bb.toString() + "\t庫存:" + tm.get(key));
}
}
}
自定義比較器和測(cè)試類:
package com.tjcyjd.comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
public class UseComparator {
public static void main(String args[]) {
List<Book> list = new ArrayList<Book>(); // 數(shù)組序列
Book b1 = new Book(10000, "紅樓夢(mèng)", 150.86, new GregorianCalendar(2009,
01, 25), "曹雪芹、高鄂");
Book b2 = new Book(10001, "三國演義", 99.68, new GregorianCalendar(2008, 7,
8), "羅貫中 ");
Book b3 = new Book(10002, "水滸傳", 100.8, new GregorianCalendar(2009, 6,
28), "施耐庵 ");
Book b4 = new Book(10003, "西游記", 120.8, new GregorianCalendar(2011, 6,
8), "吳承恩");
Book b5 = new Book(10004, "天龍八部", 10.4, new GregorianCalendar(2011, 9,
23), "搜狐");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
// Collections.sort(list); //沒有默認(rèn)比較器,不能排序
System.out.println("數(shù)組序列中的元素:");
myprint(list);
Collections.sort(list, new PriceComparator()); // 根據(jù)價(jià)格排序
System.out.println("按書的價(jià)格排序:");
myprint(list);
Collections.sort(list, new CalendarComparator()); // 根據(jù)時(shí)間排序
System.out.println("按書的出版時(shí)間排序:");
myprint(list);
}
// 自定義方法:分行打印輸出list中的元素
public static void myprint(List<Book> list) {
Iterator it = list.iterator(); // 得到迭代器,用于遍歷list中的所有元素
while (it.hasNext()) {// 如果迭代器中有元素,則返回true
System.out.println("\t" + it.next());// 顯示該元素
}
}
// 自定義比較器:按書的價(jià)格排序
static class PriceComparator implements Comparator {
public int compare(Object object1, Object object2) {// 實(shí)現(xiàn)接口中的方法
Book p1 = (Book) object1; // 強(qiáng)制轉(zhuǎn)換
Book p2 = (Book) object2;
return new Double(p1.price).compareTo(new Double(p2.price));
}
}
// 自定義比較器:按書出版時(shí)間來排序
static class CalendarComparator implements Comparator {
public int compare(Object object1, Object object2) {// 實(shí)現(xiàn)接口中的方法
Book p1 = (Book) object1; // 強(qiáng)制轉(zhuǎn)換
Book p2 = (Book) object2;
return p2.calendar.compareTo(p1.calendar);
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot web項(xiàng)目打jar或者war包并運(yùn)行的實(shí)現(xiàn)
這篇文章主要介紹了springboot web項(xiàng)目打jar或者war包并運(yùn)行的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
springboot使用redis緩存亂碼(key或者value亂碼)的解決
在通過springboot緩存數(shù)據(jù)的時(shí)候,發(fā)現(xiàn)key是一堆很不友好的東西,本文主要介紹了springboot使用redis緩存亂碼(key或者value亂碼)的解決,感興趣的可以了解一下2023-11-11
關(guān)于jackson序列化和feign返回值的問題
這篇文章主要介紹了關(guān)于jackson序列化和feign返回值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
JavaWeb開發(fā)入門第二篇Tomcat服務(wù)器配置講解
JavaWeb開發(fā)入門第二篇主要介紹了Tomcat服務(wù)器配置的方法教大家如何使用Tomcat服務(wù)器,感興趣的小伙伴們可以參考一下2016-04-04
springboot?整合?clickhouse的實(shí)現(xiàn)示例
本文主要介紹了springboot?整合?clickhouse的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
手把手教你使用IDEA創(chuàng)建多模塊(maven)項(xiàng)目
這篇文章主要給大家介紹了關(guān)于如何使用IDEA創(chuàng)建多模塊(maven)項(xiàng)目的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
解決java junit單元測(cè)試@Test報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決java junit單元測(cè)試@Test報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
SpringBoot單元測(cè)試框架Mockito介紹及使用
與集成測(cè)試將系統(tǒng)作為一個(gè)整體測(cè)試不同,單元測(cè)試更應(yīng)該專注于某個(gè)類。所以當(dāng)被測(cè)試類與外部類有依賴的時(shí)候,尤其是與數(shù)據(jù)庫相關(guān)的這種費(fèi)時(shí)且有狀態(tài)的類,很難做單元測(cè)試。但好在可以通過“Mockito”這種仿真框架來模擬這些比較費(fèi)時(shí)的類,從而專注于測(cè)試某個(gè)類內(nèi)部的邏輯2023-01-01
詳解Vue響應(yīng)式的部分實(shí)現(xiàn)
響應(yīng)式,簡(jiǎn)單來說當(dāng)數(shù)據(jù)發(fā)生變化時(shí),對(duì)數(shù)據(jù)有依賴的代碼會(huì)重新執(zhí)行。這篇文章主要為大家介紹了Vue中響應(yīng)式的部分實(shí)現(xiàn),感興趣的可以了解一下2022-12-12

