玩转java面向函数编程lambda表达式Function,Consumer,Predicate,Supplier

上代码:demo

  private static final Set<Object> setOs = Sets.newLinkedHashSet();

    /**
     * Applies this function to the given argument.
     *
     * @param accountId ->id
     * @param judge     ->test
     * @param find      ->apply
     * @param excute    ->apply
     * @param set       ->accept
     * @param get       ->get
     * @return T
     */
    private static <T> T apply4(String accountId,
                                Predicate<String> judge,
                                Function<String, Object> find,
                                Function<Object, T> excute,
                                Consumer<T> set,
                                Supplier<T> get) {
        T t = null;
        if (judge.test(accountId)) {
            t = excute.apply(find.apply(accountId));
            set.accept(t);
            t = get.get();
        } else {
            System.out.println("");
        }
        return t;
    }
    @SuppressWarnings("unchecked")
    private static void getApply4Lamda() {
        String id = "";
        Entity entity = new Entity();
        entity.setId("1111");
        Entity2 result = apply4(id,
                (Predicate) o -> entity.getColor(id),
                (Function) o -> {
                    System.out.println("find:\n");
                    return entity.getId(id);
                },
                (Function) o -> {
                    System.out.println("excute:\n");
                    return entity.getIdd((Entity) o);
                },
                (Consumer) setOs::add,
                () -> (Entity2) setOs.iterator().next()
        );
        System.out.println(result);
    }

    private static void getApply4() {
        String id = "";
        Entity entity = new Entity();
        entity.setId("1111");
        Entity2 result = apply4(id,
                //test
                new Predicate() {
                    @Override
                    public boolean test(Object o) {
                        return entity.getColor(id);
                    }
                },
                /**
                 * find  这里方法参数<object> 是object 和string  达到同样效果
                 */
                //find
                new Function() {
                    @Override
                    public Object apply(Object o) {
                        System.out.println("find:\n");
                        return entity.getId(id);
                    }
                },
                //excute
                new Function() {
                    @Override
                    public Entity2 apply(Object o) {
                        System.out.println("excute:\n");
                        return entity.getIdd((Entity) o);
                    }
                },

                new Consumer() {
                    @Override
                    public void accept(Object o) {
                        setOs.add(o);
                    }
                },
                new Supplier<Entity2>() {
                    @Override
                    public Entity2 get() {
                        return (Entity2) setOs.iterator().next();
                    }
                }
        );
        System.out.println(result);
    }

    public static void main(String[] fdfd) {
        getApply4();
    }

 

2020-3-13

utils:

    /**
     * Applies this function to the given argument.
     * function do function
     *
     * @param accountId ->id
     * @param judge     ->test
     * @param find      ->apply
     * @param excute    ->apply
     * @param set       ->accept
     * @param get       ->get
     * @return T
     */
    private static <T> T apply4(String accountId,
                                Predicate<String> judge,
                                Function<String, Object> find,
                                Function<Object, T> excute,
                                Consumer<T> set,
                                Supplier<T> get) {
        T t = null;
        if (judge.test(accountId)) {
            t = find.andThen(excute).apply(accountId);
            set.accept(t);
            t = get.get();
        } else {
            System.out.println("");
        }
        return t;
    }

    /**
     * 简单一步
     */
    protected <T> T etract(Object V, Function<Object, T> etract) {
        return etract.apply(V);
    }

    /**
     * 循环执行
     */
    private static <T> T applyWhile(
            Object object,
            Predicate<T> judge,
            Function<Object, T> excute) {
        T t = excute.apply(object);
        while (judge.test(t)) {
            t = excute.apply(t);
        }
        return t;
    }


    /**
     * 2个入参
     */
    protected static <T> T apply2excute(Object V, Object V2, BiFunction<Object, Object, T> excute) {
        return excute.apply(V, V2);
    }


    /**
     comparator :对任意类型集合对象进行整体排序,排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法排序.
     实现int compare(T o1, T o2);方法,返回正数,零,负数各代表大于,等于,小于
     */
    private static <T> T apply5(String accountId,
                                Predicate<String> judge,
                                Function<String, Object> find,
                                Function<Object, T> excute,
                                Consumer<T> set,
                                Supplier<T> get,
                                Comparator<T> comparator) {
        T t = null;
        if (judge.test(accountId)) {
            t = find.andThen(excute).apply(accountId);
            set.accept(t);
            T  t2 = get.get();
            int i=comparator.compare(t,t2);
            if(0<i){
                System.out.println(t);
            }else {
                System.out.println(t2);
            }
        } else {
            System.out.println("");
        }
        return t;
    }

 

2020-4-8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值