遊ぶjavaは関数に向かってlambda式Functionをプログラミングして,Consumer,Predicate,Supplier

6071 ワード

上コード: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:
"); return entity.getId(id); }, (Function) o -> { System.out.println("excute:
"); 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:
"); return entity.getId(id); } }, //excute new Function() { @Override public Entity2 apply(Object o) { System.out.println("excute:
"); 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