使用java8对List<Map<String,Object>>类型以及List<实体类>进行排序

更新时间:2023-05-26 23:46:27 阅读: 评论:0

为人处事的技巧
使⽤java8对List<Map<String,Object>>类型以及List<实体类>进⾏排序对ListMap进⾏排序
kill switch@Test
public void test1(){
//根据map中的某个字段值排序
Map<String,Object> map1 = new HashMap<>();
map1.put("SUM",333.123);
map1.put("NAME","张1");
Map<String,Object> map2 = new HashMap<>();
map2.put("SUM",1333.123);
map2.put("NAME","张2");
Map<String,Object> map3 = new HashMap<>();
名著下载
map3.put("SUM1",33.123);
map3.put("NAME","张3");
Map<String,Object> map4 = new HashMap<>();
map4.put("SUM",3.123);
map4.put("NAME","张4");
List<Map<String,Object>> list= Arrays.asList(map1,map2,map3,map4);
System.out.println(list);//[{SUM=333.123, NAME=张1}, {SUM=1333.123, NAME=张2}, {SUM1=33.123, NAME=张3}, {SUM=3.123, NAME=张4}]
//java8 代码
List<Map<String,Object>> list2 = list.stream().sorted((e1,e2)->{
if(!e1.containsKey("SUM")){
e1.put("SUM", 0.00);
}
if(!e2.containsKey("SUM")){
mark bradshaw
e2.put("SUM", 0.00);
}
pare((("SUM"),(Double) e2.get("SUM")); //倒序在pare前加⼀个负号‘-’
}).List());
System.out.println(list2);  //[{SUM1=33.123, SUM=0.0, NAME=张3}, {SUM=3.123, NAME=张4}, {SUM=333.123, NAME=张1}, {SUM=1333.123, NAME=张2} ]
}
对List<;实体类>排序agiler
mob//  对实例类进⾏排序
List<Employee> emps = Arrays.asList(
genocidenew Employee("张三",18,999.99,Status.BUSY),
new Employee("张四",36,9992341.99,Status.FREE),
new Employee("张五",28213,9199.99,Status.FREE),
boy gaynew Employee("张六",183121,99.2,Status.VOCATION),
new Employee("张七",823,10.3,Status.FREE),
new Employee("张七",82,10.1,Status.VOCATION),
new Employee("张七",82,10.2,Status.BUSY)
日耳曼人英语
);
System.out.println(emps);//[Employee [id=null, name=张三, age=18, salary=999.99, status=BUSY], Employee [id=null, name=张四, age=36, salary=99923 41.99, status=FREE], Employee [id=null, name=张五, age=28213, salary=9199.99, status=FREE], Employee [id=null, name=张六, age=1831
21, salary=99. 2, status=VOCATION], Employee [id=null, name=张七, age=823, salary=10.3, status=FREE], Employee [id=null, name=张七, age=82, salary=10.1, status=
VOCATION], Employee [id=null, name=张七, age=82, salary=10.2, status=BUSY]]
watch sisterList<Employee> emps2 = emps.stream().sorted((e1,e2)-> pare((Salary(),(Double) e2.getSalary())).List());  System.out.println(emps2);//[Employee [id=null, name=张七, age=82, salary=10.1, status=VOCATION], Employee [id=null, name=张七, age=82, salary=10 .2, status=BUSY], Employee [id=null, name=张七, age=823, salary=10.3, status=FREE], Employee [id=null, name=张六, age=183121, salary=99.2, status= VOCATION], Employee [id=null, name=张三, age=18, salary=999.99, status=BUSY], Employee [id=null, name=张五, age=28213, salary=9199.99, status=F REE], Employee [id=null, name=张四, age=36, salary=9992341.99, status=FREE]]

本文发布于:2023-05-26 23:46:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/788330.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:排序   实体类   名著   负号   下载
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图