很多人对list集合排序时喜欢实现Comparator<T>接口,自己定义排序方式,例如:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(7);
list.add(3);
list.add(6);
list.add(5);
list.add(5);
System.out.println("排序前:");
for (int i : list) {System.out.println(i);
}
Collections.sort(list, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1-o2;}
});
System.out.println("排序后:");
for (int i : list) {System.out.println(i);
}

输出信息:

排序前:
1
7
3
6
5
5
排序后:
1
3
5
5
6
7

这样没有问题,但是如果int换成long呢?只有改成这样:

Collections.sort(list, new Comparator<Long>() {@Overridepublic int compare(Long l1, Long l2) {return (int) (l1-l2);}
});

输出信息:

排序前:
1
7
3
6
5
5
排序后:
1
3
5
5
6
7

这样看上去也没有问题,但是如果long的数值过大呢?比如数据添加两行:

List<Long> list = new ArrayList<>();
list.add(1L);
list.add(7L);
list.add(3L);
list.add(6L);
list.add(5L);
list.add(5L);
list.add(1247189571876180L);
list.add(52856189568195L);

然而打印便成了这样:
排序前:
1
7
3
6
5
5
1247189571876180
52856189568195
排序后:
52856189568195
1247189571876180
1
3
5
5
6
7

这是为什么呢?这是因为52856189568195这样的数字与1这样的数字做减法运算后他的数值超过了int的范围,做强转时造成了数据丢失,所以我们这样做减法再强转的方式明显不可取,所以我们可以用这样的方式去解决这个问题:

Collections.sort(list, new Comparator<Long>() {@Overridepublic int compare(Long l1, Long l2) {return Long.compare(l1,l2);}
});

打印信息便正常了:

排序前:
1
7
3
6
5
5
1247189571876180
52856189568195
排序后:
1
3
5
5
6
7
52856189568195
1247189571876180

这个方法不是Long独有的,Integer,Double甚至String都有,这个方法也可以写成这样:l1.compareTo(l2),特别注意的是String,它没有compare方法,只有compareTo方法,为什么使用这个方法就可以避免数据丢失呢?我们来看源码:

public static int compare(long x, long y) {return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
public int compareTo(Long anotherLong) {return compare(this.value, anotherLong.value);
}

源码中没有进行强转,用的是一个三元运算符,这个相信大家都看得懂,属于java基础内容,我就不献丑去解释了,到这里就会有人说了,我为什么要用差距这么大数去排序呢?emmm...别急,继续往下看:

有这么一个较复杂的数据:

public class TestBean {private int id;private String name;private Date createDate;private Date updateDate;public TestBean(int id, String name, Date createDate, Date updateDate) {this.id = id;this.name = name;this.createDate = createDate;this.updateDate = updateDate;}
}

我们想对它进行排序,按创建时间排序,创建时间一样按姓名排序,我们就可以这么写:

List<TestBean> list = new ArrayList<>();
list.add(new TestBean(1,"德玛西亚刘德华",new Date(0),new Date(0)));
list.add(new TestBean(2,"艾欧里亚张学友",new Date(System.currentTimeMillis()),new Date(0)));
list.add(new TestBean(3,"诺克萨斯彭于晏",new Date(1213846185668L),new Date(0)));
list.add(new TestBean(4,"弗雷尔卓德黎明",new Date(13575917L),new Date(0)));
list.add(new TestBean(5,"皮城美少女战士",new Date(13575917L),new Date(0)));
System.out.println("排序前:");
for (TestBean b : list) {System.out.println(b.toString());
}
Collections.sort(list, new Comparator<TestBean>() {@Overridepublic int compare(TestBean b1, TestBean b2) {if (b1.getCreateDate().getTime()-b2.getCreateDate().getTime()!=0){return Long.compare(b1.getCreateDate().getTime(),b2.getCreateDate().getTime());}else {return b1.getName().compareTo(b2.getName());}}
});
System.out.println("排序后:");
for (TestBean i : list) {System.out.println(i.toString());
}

打印信息为:

排序前:
TestBean{id=1, name='德玛西亚刘德华', createDate=Thu Jan 01 08:00:00 CST 1970, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=2, name='艾欧里亚张学友', createDate=Tue Dec 11 22:36:01 CST 2018, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=3, name='诺克萨斯彭于晏', createDate=Thu Jun 19 11:29:45 CST 2008, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=4, name='弗雷尔卓德黎明', createDate=Thu Jan 01 11:46:15 CST 1970, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=5, name='皮城美少女战士', createDate=Thu Jan 01 11:46:15 CST 1970, updateDate=Thu Jan 01 08:00:00 CST 1970}
排序后:
TestBean{id=1, name='德玛西亚刘德华', createDate=Thu Jan 01 08:00:00 CST 1970, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=4, name='弗雷尔卓德黎明', createDate=Thu Jan 01 11:46:15 CST 1970, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=5, name='皮城美少女战士', createDate=Thu Jan 01 11:46:15 CST 1970, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=3, name='诺克萨斯彭于晏', createDate=Thu Jun 19 11:29:45 CST 2008, updateDate=Thu Jan 01 08:00:00 CST 1970}
TestBean{id=2, name='艾欧里亚张学友', createDate=Tue Dec 11 22:36:01 CST 2018, updateDate=Thu Jan 01 08:00:00 CST 1970}

你们看,这样不但代码简洁,而且出现错误的几率也更小是不是?本人也是个新人程序员,说的不对的地方大家可以指正,希望我们可以一起努力,早日成为大家都想成为的大神!加油,(=゚ω゚)ノ ---===≡≡≡

List集合排序Collections.sort()方法的一个容易忽略的小问题相关推荐

  1. 集合排序 Collections.sort用法

    大半夜,一同学来问这段代码.问了三个问题. 一,这个排序Sort()怎么用?  二,接口作为方法的参数是什么意思? 三,入参直接new Comparator(){}是怎么回事? 先回答第二,三个问题: ...

  2. Collections.sort()方法给集合排序

    Collections.sort()方法给集合排序 前言 因为Collections里面的sort()方法是静态方法,所以可以直接类名打点调用sort方法 Collections.sort()有两种参 ...

  3. 深入分析集合List的排序Collections.sort

    List接口本身未提供sort的方法. 在jdk中提供了一个集合操作工具类Collections来操作集合. 查看Collections类,可以发下如下两个方法: static <T exten ...

  4. JAVA-List排序-Collections.sort()-对象数组(集合)根据某一属性排序

    JAVA-List排序-Collections.sort() 当我们想对一个对象数组(集合)根据某一属性进行排序时,我们可以使用list中的Collection.sort(),这是一种较快捷的方式. ...

  5. Collections.sort()方法对象排序

    Collections.sort()方法可以对List对象进行排序,用户需要按特定属性进行排序,有两种实现方法: 1. public static <T extends Comparable&l ...

  6. 怎么Collections.sort()方法进行List排序

    一.Collections.sort()从小到大排序:Collections.sort(list); List<Integer> list = new ArrayList<Integ ...

  7. ajax id sort,ajax返回的json内容进行排序使用sort()方法实现

    ajax返回的json内容进行排序使用sort()方法实现 关键方法:sort()用于对数组的元素进行排序. return a.num-b.num是升序: return b.num-a.num;是降序 ...

  8. [转载] Python列表排序 list.sort方法和内置函数sorted

    参考链接: Python中的函数 Python列表排序 list.sort方法和内置函数sorted 很多时候我们获取到一个列表后,这个列表并不满足我们的需求,我们需要的是一个有特殊顺序的列表. 这时 ...

  9. Python列表排序 list.sort方法和内置函数sorted

    Python列表排序 list.sort方法和内置函数sorted 很多时候我们获取到一个列表后,这个列表并不满足我们的需求,我们需要的是一个有特殊顺序的列表. 这时候就可以使用list.sort方法 ...

  10. 利用random 的randint 方法写一个猜数字的小游戏

    学习python第二天,编写的一款数字小游戏 昨天学习完条件语句和while循环,老师留下的作业:利用random 的randint 方法写一个猜数字的小游戏. 第一次在CSDN上记录,小白一枚.希望 ...

最新文章

  1. 编码小记(未整理-持续更新)
  2. 【C 语言】文件操作 ( 读文本文件 | 文本加密解密 | fgets 函数 | fputs 函数 )
  3. Javascript之 对象和原型
  4. github上的linux项目,克隆GitHub上项目的非Master分支
  5. java发送gmail_如何在Gmail中轻松通过电子邮件发送人群
  6. 标签编辑新工具:如何使用控制台标签编辑器(Tag editor)
  7. 解析鸿蒙内核消息队列QueueMail接口的哼哈二将
  8. android 回退函数,android浏览器研究-回退和前进
  9. 布尔运算,二进制和门电路
  10. Bailian4029 数字反转【进制】(POJ NOI0105-29)
  11. 【2017-12-06】c#基础-分支语句and循环语句
  12. Paxos—以选美比赛为例PPT
  13. 学习 灰色2017.12.08
  14. hdoj1043 Eight(逆向BFS+打表+康拓展开)
  15. 不同颜色蔬菜代表什么营养?
  16. 滴滴WebApp实践经验分享
  17. 《他是谁》爆火,优酷的成功并非偶然
  18. 商之翼小京东+ucenter1.6.0+discuz3.3整合经历
  19. A 暴力搜索 剪枝是关键
  20. 6-禅宗的形成及其基本观念

热门文章

  1. 南京信息工程大学计算机考研怎么样,南京信息工程大学就业怎么样,考研好不好?...
  2. CVE-2022-28512 Fantastic Blog CMS 存在SQL注入漏洞
  3. 基于短时时域处理中短时能量和过零率的语音端点检测方法
  4. 手机没网了,却还能支付,这是什么原理?
  5. Centos7之LVM(逻辑卷管理器)
  6. Adb连不上夜神模拟器的原因adb devices显示设备信息为空
  7. 【计算机系统结构】第3章 流水线技术问答题
  8. Keyshot渲染-关于导入提示:未检索到几何图形的解决方法。
  9. mysql 语法大全
  10. Qt之QGraphicsView入门篇