jdk8排序功能

public static void getUser() {
        User u1 = new User();
        User u2 = new User();
        User u3 = new User();
        User u4 = new User();
        u1.setId("1");u1.setUserName("10班");
        u2.setId("13");u2.setUserName("114班");
        u3.setId("1");u3.setUserName("103班");
        u4.setId("333");u4.setUserName("123班");

        //1。 先按id排序,在按name排序
        List list1 = new ArrayList();
        list1.add(u1); list1.add(u2);list1.add(u3);list1.add(u4);
        Long l1 = System.currentTimeMillis();
        Object list11 = list1.stream().
                sorted(Comparator.comparing(User::getId,Comparator.comparingInt(Integer::parseInt)).
                        thenComparing(User::getUserName,Comparator.comparingInt(c -> Integer.parseInt(c.replace("班","")))))
                .collect(Collectors.toList());
        Long l2 = System.currentTimeMillis();
        System.out.println("双排--1:"+list11.toString() + "---:" + (l2-l1));

        //2。 常用
        List list2 = new ArrayList();
        list2.add(u1); list2.add(u2);list2.add(u3);list2.add(u4);
        Long l3 = System.currentTimeMillis();
        list2.sort(Comparator.comparing(User::getId).
                thenComparing(User::getUserName,Comparator.comparingInt(c -> Integer.parseInt(c.replace("班","")))));
        Long l4 = System.currentTimeMillis();
        System.out.println("双排--2:"+list2.toString() + "---:" + (l4-l3));
        // list2.forEach(System.out::println);

        //3。 先按id排序,在按name排序(无效)
        List list3 = new ArrayList();
        list3.add(u1); list3.add(u2);list3.add(u3);list3.add(u4);
        Long l5 = System.currentTimeMillis();
        Collections.sort(list3,Comparator.comparing(User::getId));
        Collections.sort(list3,Comparator.comparing(User::getUserName, Comparator.comparingInt(c -> Integer.parseInt(c.replace("班", "")))));
        Long l6 = System.currentTimeMillis();
        System.out.println("双排--3:"+list3.toString() + "---:" + (l6-l5));

        //4。 先按id排序,在按name排序(无效)
        List list4 = new ArrayList();
        list4.add(u1); list4.add(u2);list4.add(u3);list4.add(u4);
        Long l7 = System.currentTimeMillis();
        list4.sort(Comparator.comparing(User::getId));
        list4.sort(Comparator.comparing(User::getUserName, Comparator.comparingInt(c -> Integer.parseInt(c.replace("班", "")))));
        Long l8 = System.currentTimeMillis();
        System.out.println("双排--4:"+list4.toString() + "---:" + (l8-l7));

        // ----------- 简单ID排序---------------
        // -1
        List listD1 = new ArrayList();
        listD1.add(u1); listD1.add(u2);listD1.add(u3);listD1.add(u4);
        Collections.sort(listD1, new Comparator() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });
        System.out.println("单排--1:"+listD1);

        // -2
        List listD2 = new ArrayList();
        listD2.add(u1); listD2.add(u2);listD2.add(u3);listD2.add(u4);
        Collections.sort(listD2,(User a, User b) -> {
            return a.getId().compareTo(b.getId());
        });
        System.out.println("单排--2:"+listD2);

        // -3
        List listD3 = new ArrayList();
        listD3.add(u1); listD3.add(u2);listD3.add(u3);listD3.add(u4);
        Collections.sort(listD3,(User a, User b) -> a.getId().compareTo(b.getId()));
        System.out.println("单排--3:"+listD3);

        // -4
        List listD4 = new ArrayList();
        listD4.add(u1); listD4.add(u2);listD4.add(u3);listD4.add(u4);
        Collections.sort(listD4,(a, b) -> a.getId().compareTo(b.getId()));
        System.out.println("单排--4:"+listD4);

        // 常用
        List listD5 = new ArrayList();
        listD5.add(u1); listD5.add(u2);listD5.add(u3);listD5.add(u4);
        Collections.sort(listD5, Comparator.comparing(User::getId));
        System.out.println("单排--5:"+listD5);

        // 常用
        List listD6 = new ArrayList();
        listD6.add(u1); listD6.add(u2);listD6.add(u3);listD6.add(u4);
        listD6.sort(Comparator.comparing(User::getId));
        System.out.println("单排--6:"+listD6);
    }

常用:

 

jdk8排序功能_第1张图片

你可能感兴趣的:(java)