Java8对list进行排序的两个写法,包含了纯字符串list和对象list写法

// reversed为倒序,不加则正序
List<String> maxUpdateTime = updateTimeList.stream()

         .sorted(
                Comparator.comparing(
                      s -> DateUtil.convertStringToDate(
                           s.toString(),"yyyy-MM-dd HH:mm:ss")
                                 .getTime()).reversed()
         
         ).collect(Collectors.toList());

// 按照日期排序
List<String> maxUpdateTime = updateTimeList.stream()

        .sorted(
            new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    try {
                         Date d1 = DateUtil.convertStringToDate(o1, "yyyy-MM-dd HH:mm:ss");
                         Date d2 = DateUtil.convertStringToDate(o2, "yyyy-MM-dd HH:mm:ss");
                         //正序
                         //return d1.compareTo(d2);
                         //倒序
                         return d2.compareTo(d1);
                     } catch (Exception e) {
                         e.printStackTrace();
                }
                return 0;
            }
        }).collect(Collectors.toList());

list.parallelStream().sorted((o1, o2) -> o1.getOperTime().compareTo(o2.getOperTime()));
等于
list.parallelStream().sorted(Comparator.comparing(JfBlackError::getOperTime()));


扫描二维码,在手机上阅读!

发表评论

电子邮件地址不会被公开。 必填项已用*标注