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

上代码:demo

  private static final Set 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 apply4(String accountId,
                                Predicate judge,
                                Function find,
                                Function excute,
                                Consumer set,
                                Supplier 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 和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() {
                    @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 apply4(String accountId,
                                Predicate judge,
                                Function find,
                                Function excute,
                                Consumer set,
                                Supplier 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 etract(Object V, Function etract) {
        return etract.apply(V);
    }

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


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


    /**
     comparator :对任意类型集合对象进行整体排序,排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法排序.
     实现int compare(T o1, T o2);方法,返回正数,零,负数各代表大于,等于,小于
     */
    private static  T apply5(String accountId,
                                Predicate judge,
                                Function find,
                                Function excute,
                                Consumer set,
                                Supplier get,
                                Comparator 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

 

2020-4-8

你可能感兴趣的:(玩转java面向函数编程lambda表达式Function,Consumer,Predicate,Supplier)