我是靠谱客的博主 坚定面包,最近开发中收集的这篇文章主要介绍使用java8 对List<Map<String,Object>>类型以及List<实体类>进行排序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

对ListMap进行排序

@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")) {
					e2.put("SUM", 0.00);
				}
				return Double.compare((Double)e1.get("SUM"),(Double) e2.get("SUM")); //倒序在Double.compare前加一个负号‘-’
				
			}).collect(Collectors.toList());
		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<实体类>排序

//		对实例类进行排序
		List<Employee> emps = Arrays.asList(
         new Employee("张三",18,999.99,Status.BUSY),
			new Employee("张四",36,9992341.99,Status.FREE),
			new Employee("张五",28213,9199.99,Status.FREE),
			new 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=9992341.99, status=FREE], Employee [id=null, name=张五, age=28213, salary=9199.99, status=FREE], Employee [id=null, name=张六, age=183121, 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]]
		List<Employee> emps2 = emps.stream().sorted((e1,e2)-> Double.compare((Double)e1.getSalary(),(Double) e2.getSalary())).collect(Collectors.toList());
		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=FREE], Employee [id=null, name=张四, age=36, salary=9992341.99, status=FREE]]

最后

以上就是坚定面包为你收集整理的使用java8 对List<Map<String,Object>>类型以及List<实体类>进行排序的全部内容,希望文章能够帮你解决使用java8 对List<Map<String,Object>>类型以及List<实体类>进行排序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部