码迷,mamicode.com
首页 > 编程语言 > 详细

Java8之集合排序

时间:2020-05-12 17:17:33      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:style   java8   rev   void   tag   student   system   倒序   pre   

1.List<Map<String,Object>>格式

Map<String,Object> map= new HashMap<>();
List<Map<String,Object>> list= new ArrayList<>();
map.put("count",1); 
//排序
Comparator<Map<String, Object>> comparator = Comparator.comparing(item -> item.get("count").toString()); list= list.stream().sorted(comparator.reversed()).collect(Collectors.toList());

2.List<Model>格式

list.sort(Comparator.comparing(Model::getOrder));

 3.Set<Model>格式

public static void test2(){
    Set<Student> students = new HashSet<>();
    Student student1 = new Student(1,"李四",1);
    Student student2 = new Student(2,"张三",3);
    Student student3 = new Student(3,"王麻",2);
    students.add(student1);
    students.add(student2);
    students.add(student3);
    List<Student> studentList1 = new ArrayList<>(students);
    studentList1.sort(Comparator.comparing(Student::getAge));
    System.out.println(studentList1);
}

4.Set<String>格式

public static void test3(){
        Set<String> sets = new HashSet<>();
        sets.add("aa");
        sets.add("ee");
        sets.add("cc");
        //倒序
        Set<String> treeSetDesc = new TreeSet<>((o1, o2) -> o2.compareTo(o1));
        treeSetDesc.addAll(sets);
        System.out.println(treeSetDesc);
        //升序
        Set<String> treeSetAsc = new TreeSet<>((o1, o2) -> o1.compareTo(o2));
        treeSetAsc.addAll(sets);
        System.out.println(treeSetAsc);
    }

 

Java8之集合排序

标签:style   java8   rev   void   tag   student   system   倒序   pre   

原文地址:https://www.cnblogs.com/bbllw/p/12877011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!