java 集合儲存物件且根據對象屬性排序

來源:互聯網
上載者:User

標籤:使用   result   java 集合   actor   ble   using   code   zh-cn   基本類型   

方法一:根據java1.8lambda運算式進行排序

Comparator<RateInfo> comparator = (t1, t2) -> t1.getRateCode().compareTo(t2.getRateCode());

方法二:使用List的方法sort()排序

List API:default void sort(Comparator<? super E> c)

其實也是依據Comarator這個類

rateInfolist.sort(comparator.reversed());

方法三:使用Collections類的sort進行排序

static <T> void sort(List<T> list, Comparator<? super T> c)

Sorts the specified list according to the order induced by the specified comparator.Google翻譯:根據指定比較子引發的順序對指定列表進行排序。其實該排序也是使用Comparator類進行排序Comparator類API:英文翻譯即可
int     compare(T o1, T o2)Compares its two arguments for order.static <T,U extends Comparable<? super U>>Comparator<T>     comparing(Function<? super T,? extends U> keyExtractor)Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.static <T,U> Comparator<T>     comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)Accepts a function that extracts a sort key from a type T, and returns a Comparator<T> that compares by that sort key using the specified Comparator.static <T> Comparator<T>     comparingDouble(ToDoubleFunction<? super T> keyExtractor)Accepts a function that extracts a double sort key from a type T, and returns a Comparator<T> that compares by that sort key.static <T> Comparator<T>     comparingInt(ToIntFunction<? super T> keyExtractor)Accepts a function that extracts an int sort key from a type T, and returns a Comparator<T> that compares by that sort key.static <T> Comparator<T>     comparingLong(ToLongFunction<? super T> keyExtractor)Accepts a function that extracts a long sort key from a type T, and returns a Comparator<T> that compares by that sort key.boolean     equals(Object obj)Indicates whether some other object is "equal to" this comparator.static <T extends Comparable<? super T>>Comparator<T>     naturalOrder()Returns a comparator that compares Comparable objects in natural order.static <T> Comparator<T>     nullsFirst(Comparator<? super T> comparator)Returns a null-friendly comparator that considers null to be less than non-null.static <T> Comparator<T>     nullsLast(Comparator<? super T> comparator)Returns a null-friendly comparator that considers null to be greater than non-null.default Comparator<T>     reversed()Returns a comparator that imposes the reverse ordering of this comparator.static <T extends Comparable<? super T>>Comparator<T>     reverseOrder()Returns a comparator that imposes the reverse of the natural ordering.default Comparator<T>     thenComparing(Comparator<? super T> other)Returns a lexicographic-order comparator with another comparator.default <U extends Comparable<? super U>>Comparator<T>     thenComparing(Function<? super T,? extends U> keyExtractor)Returns a lexicographic-order comparator with a function that extracts a Comparable sort key.default <U> Comparator<T>     thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator.default Comparator<T>     thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)Returns a lexicographic-order comparator with a function that extracts a double sort key.default Comparator<T>     thenComparingInt(ToIntFunction<? super T> keyExtractor)Returns a lexicographic-order comparator with a function that extracts a int sort key.default Comparator<T>     thenComparingLong(ToLongFunction<? super T> keyExtractor)Returns a lexicographic-order comparator with a function that extracts a long sort key.

代碼:

Collections.sort(rateInfolist, Comparator.comparing(RateInfo::getRateCode));

方法四:使用Comparator的匿名對象類重寫compare方法

代碼:

Collections.sort(rateInfolist, new Comparator<RateInfo>(){                    /*                     * int compare(RateInfo R1, RateInfo R2) 返回一個基本類型的整型,                     * 返回負數表示:R1 小於R2,                     * 返回0 表示:R1和R2相等,                     * 返回正數表示:R1大於R2                     */                    public int compare(RateInfo R1, RateInfo R2) {                        Integer rateCode1 = Integer.parseInt(R1.getRateCode());                        Integer rateCode2 = Integer.parseInt(R2.getRateCode());                        //按照RateCode的年齡進行升序排列                        if(rateCode1 > rateCode2){                            return 1;                        }                        if(rateCode1 == rateCode2){                            return 0;                        }                        return -1;                    }                });

自己寫代碼時遇到的問題,根據我的理解和網上的資料做的總結

java 集合儲存物件且根據對象屬性排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.