zoukankan      html  css  js  c++  java
  • 复合Lambda表达式

    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    import java.util.function.Function;
    import java.util.function.Predicate;
    
    /**
     * 复合Lambda表达式
     */
    public class Demo {
    
        public static <T> List<T> filter(List<T> list, Predicate<T> p ){
            List<T> res = new ArrayList<>();
            for (T t : list) {
                if (p.test(t)){
                    res.add(t);
                }
            }
            return res;
        }
    
        public static void main(String[] args){
            List<Apple> appleList = Arrays.asList(new Apple("red",12),new Apple("yellow",15),new Apple("green",10),new Apple("red",10));
            // 比较器复合
            //逆序,按重量递减排序
            appleList.sort(Comparator.comparing(Apple::getWeight).reversed());
    
    
            //比较器链
            appleList.sort(Comparator.comparing(Apple::getWeight)
                .reversed()
                .thenComparing(Apple::getColor)
            );
    
            //谓词复合, negate,and,or      非,与,或
            Predicate<Apple> redApple = apple -> "red".equals( apple.getColor());
    
            List<Apple> list = filter(appleList, redApple);
    
            Predicate<Apple> notRedApple = redApple.negate();
    
            Predicate<Apple> redAndHeavyApple = redApple.and(a -> a.getWeight()>10);
    
            Predicate<Apple> redAndHeavyAppleOrGreen = redAndHeavyApple.or(a->"green".equals(a.getColor()));
    
            List<Apple> list2 = filter(appleList, redAndHeavyAppleOrGreen);
    
            //函数复合 g(f(x))
            Function<Integer,Integer> f = x -> x + 1;
            Function<Integer,Integer> g = x -> x * 2;
            Function<Integer,Integer> h = f.andThen(g);
            int res = h.apply(1);
            System.out.println(res);
    
            //函数复合 f(g(x))
            Function<Integer,Integer> h2 = f.compose(g);
            res = h2.apply(1);
            System.out.println(res);
        }
    }
    
    
    
    import java.util.function.Function;
    
    /**
     * 函数复合
     * 模拟写一封信
     */
    public class Demo2 {
        //抬头
        public static String addHeader(String text){
            return "From Fly:
    " + text;
        }
        //落款
        public static String addFooter(String text){
            return text + "
    		Kind";
        }
        //拼写检查
        public static String checkSpelling(String text){
            return text.replaceAll("labda","lambda");
        }
        public static void main(String[] args){
            Function<String,String> addHeader = Demo2::addHeader;
            Function<String,String> transformationPipeline =
                    addHeader.andThen(Demo2::checkSpelling)
                             .andThen(Demo2::addFooter);
    
            String letter = transformationPipeline.apply("I like labda");
            System.out.println(letter);
        }
    }
    
  • 相关阅读:
    创建Variant数组
    ASP与存储过程(Stored Procedures)
    FileSystemObject对象成员概要
    Kotlin 朱涛9 委托 代理 懒加载 Delegate
    Kotlin 朱涛 思维4 空安全思维 平台类型 非空断言
    Kotlin 朱涛7 高阶函数 函数类型 Lambda SAM
    Kotlin 朱涛16 协程 生命周期 Job 结构化并发
    Proxy 代理模式 动态代理 cglib MD
    RxJava 设计理念 观察者模式 Observable lambdas MD
    动态图片 Movie androidgifdrawable GifView
  • 原文地址:https://www.cnblogs.com/fly-book/p/12640912.html
Copyright © 2011-2022 走看看