• 函数式接口,方法引用,Lambda实现的List集合排序小工具


    1.在Java8中引入了一个函数式接口Consumer的对象,该对象可以把方法作为参数进行传递。
    关于lambda的教程

    /**
     * 集合排序
     */
    @AllArgsConstructor
    public class CollSort {
    
        // 排序因子
        private final Long order;
    
        public final static CollSort DESC = new CollSort(-1L);
        public final static CollSort ASC = new CollSort(1L);
    
        // 根据数字排序
        public <E> Collection<E> sort(Collection<E> collection, ProcessorNumber<E> getParam) {
            return collection.stream()
                    .filter(Objects::nonNull)
                    .sorted((o1, o2) -> {
                        // 这里执行传入的抽象方法,并传入函数方法需要的参数,
                        // getParam的返回值已经在抽象函数方法中确定了
                        long result = Long.parseLong(String.valueOf(getParam.accept(o1))) - Long.parseLong(String.valueOf(getParam.accept(o2)));
                        result = result * order; // 排序系数介入
                        return result == 0 ? 0 : result < 0 ? -1 : 1;
                    })
                    .collect(Collectors.toList());
        }
    
        // 根据时间排序
        public <E> Collection<E> sort(Collection<E> collection, ProcessorDate<E> getParam) {
            return collection.stream()
                    .filter(Objects::nonNull) // 无效数据过滤
                    .sorted((o1, o2) -> {
                        long t1 = getParam.accept(o1).getTime(); // 这里抽象方法getParam的返回值已经在抽象函数方法中确定了
                        long t2 = getParam.accept(o2).getTime();
                        long result = t1 - t2;
                        result = result * order; // 排序系数介入
                        return result == 0 ? 0 : result < 0 ? -1 : 1;
                    })
                    .collect(Collectors.toList());
        }
    
    }
    
    @FunctionalInterface
    interface ProcessorDate<E> {
    
        /**
         * 获取对象时间属性的抽象函数方法
         *
         * @param t 对象
         * @return 对象的时间属性
         */
        Date accept(E t);
    }
    
    @FunctionalInterface
    interface ProcessorNumber<E> {
    
        /**
         * 获取对象数字属性的抽象函数方法
         *
         * @param t 对象
         * @return 对象的数字属性
         */
        Number accept(E t);
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    使用

      public static void main(String[] args) {
         SortTest sortTest = new SortTest();
         ArrayList<User> users = new ArrayList<>();
         users.add(new User("zz", 14,new Date()));
         users.add(new User("zz", 17,new Date(System.currentTimeMillis()+1000000)));
         users.add(new User("zz", 11,new Date(System.currentTimeMillis()+4000000)));
         users.add(new User("zz", 15,new Date(System.currentTimeMillis()+9000000)));
         users.add(new User("zz", 21,new Date(System.currentTimeMillis()+8000000)));
         users.add(new User("zz", 7,new Date(System.currentTimeMillis()+3000000)));
         sortTest.test(users);
     }
     public void test(List<User> users) {
         // 就普通的工具方法调用,区别就是参数是一个方法,也可以直接引用方法
         Collection<User> ageSort = CollSort.ASC.sort(users, User::getAge);
         System.out.println("年龄排序=========================");
         ageSort.forEach(System.out::println);
         System.out.println("生日排序排序======================");
         Collection<User> timeSort = CollSort.DESC.sort(users, User::getBirthDay);
         timeSort.forEach(System.out::println);
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    效果
    在这里插入图片描述
    可以根据需求自行拓展函数接口,需要注意参数类型返回值类型,大多数时候更换返回值类型就可以了;

    参数类型以及返回值类型影响这个函数方法在调用的时候需要传入的参数,以及响应值

  • 相关阅读:
    用两行代码实现重试功能,spring-retry真是简单而优雅
    贪心,队列,运算符重载,牛客:连环爆炸
    HashMap底层原理及jdk1.8源码解读
    索引待整理笔记
    前端实现菜单&按钮级权限
    面试:类相关---Java、Android有哪些类加载器
    proteus仿真-51单片机定时器程序
    HTML常用基本元素总结
    Java常见面试题
    JVM学习(三)--生产环境的线程问题诊断
  • 原文地址:https://blog.csdn.net/m0_49194578/article/details/125565602