首页
点滴
Java集合排序
#### 1、对象集合排序 假设有个用户User对象,需要按年龄age排序 ``` // 一个用户User对象的集合 List
userList = new ArrayList<>(); // 正序,按年龄从小到大排序 Collections.sort(userList,Comparator.comparing(User::getAge)); // 倒序,按年龄从大到小排序 Collections.sort(list,Comparator.comparing(User::getAge).reversed()); ``` #### 2、String集合排序 ``` List
strList = new ArrayList<>(); strList.add("dbc"); strList.add("ba"); strList.add("acas"); strList.add("e"); strList.add("cdfas"); // 正序 Collections.sort(strList); // 倒序 Collections.reverse(strList); // 按字符串长度从小到大排序 Collections.sort(strList, (x,y) -> x.length() - y.length()); // 按字符串长度从大到小排序 Collections.sort(strList, (x,y) -> y.length() - x.length()); ``` #### 3、int 集合排序 ``` List
intList = new ArrayList<>(); intList.add(4); intList.add(5); intList.add(3); intList.add(1); intList.add(2); // 正序, Collections.sort(intList); // 倒序 Collections.reverse(intList); ```
博客分类
源码解析 (1)
Java (10)
Linux (8)
多线程 (5)
Docker (9)
SpringBoot (14)
微服务 (1)
Redis (15)
MySQL (7)
VMware (3)
Nginx (15)
MyBatis (2)
Git (7)
RabbitMQ (1)
工具类 (12)
前端 (3)
友情链接
layui
© 2020-2025 www.chenhuazhan.com All Rights Reserved 备案号:
桂ICP备17004487号-1
粤公网安备44030002005146