zoukankan      html  css  js  c++  java
  • Java8中list.sort的lamba表达式

      最近写代码,需要对list集合排序,IDEA总是黄色警告:

    Reports calls to Collections.sort(list, comparator) which could be replaced with list.sort(comparator).

      楼主多少有点强迫症,那就改吧,修改如下:

                /*Collections.sort(resultList, new Comparator<SystemLog>() {
                        @Override
                        public int compare(SystemLog o1, SystemLog o2) {
                            return o2.getDt().compareTo(o1.getDt());
                        }
                    });*/
    
                    resultList.sort(new Comparator<SystemLog>(){
                        @Override
                        public int compare(SystemLog o1, SystemLog o2) {
                            return o2.getDt().compareTo(o1.getDt());
                        }
                    });        

      

      好嘛,有报了要换Lamba表达式:Reports all anonymous classes which can be replaced with lambda expressions,Lambda syntax is not supported under Java 1.7 or earlier JVMs.

       那就继续改吧,

                /*Collections.sort(resultList, new Comparator<SystemLog>() {
                        @Override
                        public int compare(SystemLog o1, SystemLog o2) {
                            return o2.getDt().compareTo(o1.getDt());
                        }
                    });*/
                    /*resultList.sort(new Comparator<SystemLog>(){
                        @Override
                        public int compare(SystemLog o1, SystemLog o2) {
                            return o2.getDt().compareTo(o1.getDt());
                        }
                    });*/
    
                    resultList.sort(((o1, o2) -> o2.getDt().compareTo(o1.getDt())));

      OK,没有提示了,代码也确实简洁很多。

  • 相关阅读:
    Max retries exceeded with ur
    DHTML【1】
    广播发送与接收
    用例图之我见
    rman catalog (rman 恢复目录)
    面试高频题:单链表的逆置操作/链表逆序
    C#3.0 语言基础扩充
    hdu 1114 Piggy-Bank(完全背包)
    VSS Get Latest Version 没有提示recursive的对话框解决
    微服务实践分享(3)服务发现
  • 原文地址:https://www.cnblogs.com/JohanChan/p/12097264.html
Copyright © 2011-2022 走看看