ht-8 对arrayList中的自定义对象排序( Collections.sort(ListT list, Comparator? super T c))...
生活随笔
收集整理的這篇文章主要介紹了
ht-8 对arrayList中的自定义对象排序( Collections.sort(ListT list, Comparator? super T c))...
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
1 package com.iotek.set; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.List; 7 /** 8 * 9 * 對(duì)ArrayList容器中的內(nèi)容進(jìn)行排序: ArrayList中存儲(chǔ)多個(gè)Person對(duì)象(具有name,age,id屬性), 10 * 要求按照年齡從小到大排序,年齡相等的話再按照名字的自然順序來(lái)排序輸出 11 * 思路: 12 * 使用ArrayList來(lái)存儲(chǔ)Person對(duì)象,使用Collections類所提供的靜態(tài)方法sort來(lái)按照要求對(duì) 13 * ArrayList進(jìn)行排序,然后輸出排序好的信息。 14 * @author Administrator 15 * 16 */ 17 public class CollectionsDemo2 { 18 /* 1.創(chuàng)建一個(gè)ArrayList容器 19 * 2.創(chuàng)建一個(gè)Person類,具有name,age,id屬性 20 * 3.對(duì)容器中的數(shù)據(jù)排序,用Collections類的方法sort對(duì)List接口的實(shí)現(xiàn)類排序 21 * 4.輸出排序好的內(nèi)容 */ 22 23 public static void main(String[] args) { 24 List<Personc> data = new ArrayList<Personc>(); 25 data.add(new Personc("jack",20,10)); 26 data.add(new Personc("rose",10,7)); 27 data.add(new Personc("mary",30,6)); 28 data.add(new Personc("zhang",50,18)); 29 data.add(new Personc("jay",20,11)); 30 Collections.sort(data, new Comparator<Personc>() { 31 @Override 32 public int compare(Personc o1, Personc o2) { 33 // 首先按年齡來(lái)排序 34 if(o1.getAge() - o2.getAge() > 0) { 35 return 1; 36 } else if(o1.getAge() - o2.getAge() < 0) { 37 return -1; 38 } else { //年齡相等時(shí),再按照名字來(lái)進(jìn)行排序, 39 /*具體的字符串是String類的實(shí)例化對(duì)象,可以調(diào)用String類的 40 * compareTo(String anotherString)方法來(lái)對(duì)字符串按照字典順序進(jìn)行排序 41 */ return o1.getName().compareTo(o2.getName()); 42 } 43 } 44 }); 45 46 for(Personc p : data) { 47 System.out.println(p.toString()); 48 } 49 } 50 51 } 52 /* sort(List<T> list, Comparator<? super T> c) 根據(jù)指定比較器產(chǎn)生的順序?qū)χ付斜磉M(jìn)行排序 53 * Comparator<? super T> c c表示一個(gè)比較器,比較器可以用匿名內(nèi)部類來(lái)實(shí)現(xiàn) 54 * 匿名內(nèi)部類產(chǎn)生的是個(gè)接口的實(shí)現(xiàn)類對(duì)象,因此要實(shí)現(xiàn)這個(gè)接口中的compare()方法 */ 55 /*String.compareTo(String anotherString) 按字典順序比較兩個(gè)字符串,返回一個(gè)int類型的值*/ 56 57 class Personc { 58 private String name; 59 private int age; 60 private int id; 61 public Personc(String name, int age, int id) { 62 super(); 63 this.name = name; 64 this.age = age; 65 this.id = id; 66 } 67 public String getName() { 68 return name; 69 } 70 public void setName(String name) { 71 this.name = name; 72 } 73 public int getAge() { 74 return age; 75 } 76 public void setAge(int age) { 77 this.age = age; 78 } 79 public int getId() { 80 return id; 81 } 82 public void setId(int id) { 83 this.id = id; 84 } 85 @Override 86 //重寫toString方法,定義打印格式 87 public String toString() { 88 return "Person [name=" + name + ", age=" + age + ", id=" + id + "]"; 89 } 90 91 }?
轉(zhuǎn)載于:https://www.cnblogs.com/enjoyjava/p/9393398.html
總結(jié)
以上是生活随笔為你收集整理的ht-8 对arrayList中的自定义对象排序( Collections.sort(ListT list, Comparator? super T c))...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 京东是什么
- 下一篇: P4127 [AHOI2009]同类分布