【笔记】java8处理List集合数据,按照集合中的指定元素进行排序

List<User> userList = new ArrayList<>();
		
User user = new User();
user.setName("zhangsan");
user.setAge(14);
user.setHeight(160);
userList.add(user);

User user2 = new User();
user2.setName("lisi");
user2.setAge(18);
user2.setHeight(179);
userList.add(user2);

User user3 = new User();
user3.setName("wanger");
user3.setAge(18);
user3.setHeight(175);
userList.add(user3);

System.out.println("===================== (排序前数据展示) =====================");
userList.forEach(u -> {
	System.out.println(u.getName() + " " + u.getAge() + "岁,身高 " + u.getHeight() + "厘米");
});

List<User> list = userList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
System.out.println("===================== (按年龄升序排列) =====================");
list.forEach(u -> {
	System.out.println(u.getName() + " " + u.getAge() + "岁,身高 " + u.getHeight() + "厘米");
});

List<User> list2 = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
System.out.println("===================== (按年龄降序排列) =====================");
list2.forEach(u -> {
	System.out.println(u.getName() + " " + u.getAge() + "岁,身高 " + u.getHeight() + "厘米");
});


List<User> list3 = userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getHeight)).collect(Collectors.toList());
System.out.println("===================== (按年龄升序排列, 按身高升序排列) =====================");
list3.forEach(u -> {
	System.out.println(u.getName() + " " + u.getAge() + "岁,身高 " + u.getHeight() + "厘米");
});

运行结果:

===================== (排序前数据展示) =====================
zhangsan 14岁,身高 160厘米
lisi 18岁,身高 179厘米
wanger 18岁,身高 175厘米
===================== (按年龄升序排列) =====================
zhangsan 14岁,身高 160厘米
lisi 18岁,身高 179厘米
wanger 18岁,身高 175厘米
===================== (按年龄降序排列) =====================
lisi 18岁,身高 179厘米
wanger 18岁,身高 175厘米
zhangsan 14岁,身高 160厘米
===================== (按年龄升序排列, 按身高升序排列) =====================
zhangsan 14岁,身高 160厘米
wanger 18岁,身高 175厘米
lisi 18岁,身高 179厘米

补充 2022-6-22

对List> 排序(升序)

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "zhangsan");
map1.put("age", "12");
list.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "tom");
map2.put("age", "5");
list.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("name", "jack");
map3.put("age", "31");
list.add(map3);

Collections.sort(list, (o1, o2) -> {
	Integer age1 = Integer.valueOf(o1.get("age").toString()) ; //取list第一个map元素的age值
    Integer age2 = Integer.valueOf(o2.get("age").toString()) ; //取list第二个map元素的age值
    return age1.compareTo(age2);
});

for (Map<String, Object> map : list) {
    System.out.println(map.get("age"));
}

输出

5
12
31

如果要降序排序,只需改变上面 age1.compareTo(age2)age2.compareTo(age1),即

Collections.sort(list, (o1, o2) -> {
	Integer age1 = Integer.valueOf(o1.get("age").toString()) ; //取list第一个map元素的age值
    Integer age2 = Integer.valueOf(o2.get("age").toString()) ; //取list第二个map元素的age值
    return age2.compareTo(age1);
});

你可能感兴趣的:(Java,java)