收藏 分销(赏)

java-Collections.sort实现List排序的默认方法和自定义方法.doc

上传人:仙人****88 文档编号:11890775 上传时间:2025-08-18 格式:DOC 页数:5 大小:33KB 下载积分:10 金币
下载 相关 举报
java-Collections.sort实现List排序的默认方法和自定义方法.doc_第1页
第1页 / 共5页
java-Collections.sort实现List排序的默认方法和自定义方法.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
java Collections.sort()实现List排序的默认方法和自定义方法 1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list.add("王硕"); list.add("李明"); list.add("刘迪"); list.add("刘布"); //升序 Collections.sort(list,Collator.getInstance(java.util.Locale.CHINA));//注意:是根据的汉字的拼音的字母排序的,而不是根据汉字一般的排序方法 for(int i=0;i<list.size();i++) {     System.out.print(list.get(i)); } System.out.println(""); //降序 Collections.reverse(list);//不指定排序规则时,也是按照字母的来排序的 for(int i=0;i<list.size();i++) {      System.out.print(list.get(i)); } 输出结果: 李明刘布刘迪刘媛媛王硕 王硕刘媛媛刘迪刘布李明   2.自定义的排序规则: 第一种是model类实现Comparable接口,重写重写int compareTo(Object o)方法             model类: public class StudentDTO implements Comparable {  private String name;  private int age; public String getName() {     return name; } public void setName(String name) {      this.name = name; } public ObjType getType() {     return type; } public void setAge(int age) {      this.age= age; } @Override public int compareTo(Object o) {        StudentDTO sdto = (StudentDTO)o;        int otherAge = sdto.getAge();       // note: enum-type's comparation depend on types' list order of enum method       // so, if compared property is enum-type ,then its comparationfollow ObjEnum.objType order       return pareTo(otherAge); } }            主方法:            public static void main(String[] args) {       List<StudentDTO> studentList = new ArrayList();       StudentDTO s1 = new StudentDTO ();       s.setName("yuanyuan");       s.setAge(22);       studentList.add(s1);                   StudentDTO s1 = new StudentDTO ();                   s.setName("lily");                   s.setAge(23);                   studentList.add(s2);                   Collections.sort(studentList);  //按照age升序 22,23,                  Collections.reverse(studentList);  //按照age降序 23,22    } 第二种是比较器类实现Comparator接口,重写int compare(Object o1, Object o2)方法;            model类:            public class StudentDTO implements Comparable {      private String name;      private int age;      public String getName()      {          return name;      }      public void setName(String name)     {          this.name = name;      }      public ObjType getType()      {          return type;      }      public void setAge(int age)      {          this.age= age;      } }             比较器类: class MyCompartor implements Comparator {      @Override      public int compare(Object o1, Object o2)     {            StudentDTO sdto1= (StudentDTO )o1;            StudentDTO sdto2= (StudentDTO )o2;            return sdto1.getApareTo(stdo2.getAge())     } }            主方法: public static void main(String[] args) {       List<StudentDTO> studentList = new ArrayList();       StudentDTO s1 = new StudentDTO ();       s.setName("yuanyuan");       s.setAge(22);       studentList.add(s1);       StudentDTO s1 = new StudentDTO ();       s.setName("lily");       s.setAge(23);       studentList.add(s2);       MyComparetor mc = new MyComparetor();       Collections.sort(studentList,mc);     //按照age升序 22,23,       Collections.reverse(studentList,mc);    //按照age降序 23,22    }     附注: 1.对于数组的排序方法如下: String[] names = {"王林",  "杨宝", "李镇", "刘迪", "刘波"};   Arrays.sort(names, com.ibm.icu.text.Collator.getInstance(com.ibm.icu.util.ULocale.SIMPLIFIED_CHINESE));//升序;    System.out.println(Arrays.toString(names));       2.对于汉字的排序:可以尝试使用ICU4J会得到更好的结果,特别是姓为某些生僻字的时候, 用com.ibm.icu.text.Collator替换java.text.Collator,用com.ibm.icu.util.ULocale替换java.util.Locale 3.对于枚举类型的pareTo(enum2)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大, 而不是按照值的字母先后顺序比较的。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服