Java8 函数式接口扫盲

2021-07-02  本文已影响0人  WebGis学习笔记

import java.util.Comparator;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName MyFunction
 * @Author Javan
 * @Date 2021/6/30 14:54
 * @Description
 *
 * Function:            接收一个参数          object
 * Consumer:            接收一个参数          void
 * Predicate:           接收一个参数          boolean
 * UnaryOperator:       接收一个参数          object
 * BinaryOperator:      接收两个参数          object
 *
 **/
public class MyFunction {

    public interface MyConsumer<T, U, R, S, G>{
        void accept(T t,U u,R r,S s,G g);
        static MyConsumer print() {
            return (p1,p2,p3,p4,p5) -> System.err.println("MyConsumer print -> " + Stream.of(p1,p2,p3,p4,p5).map(String::valueOf).collect(Collectors.joining(",")));
        }
    }

    private static Function f = ((Function) t -> t)
            .andThen(t -> {
                System.err.println(t);
                return t + "then -> ";
            }).andThen(t -> {
                System.err.println(t);
                return t + "then1 -> ";
            }).compose(d -> {
                System.err.println(d);
                return d + "compose -> ";
            }).compose(d -> {
                System.err.println(d);
                return d + "compose1 -> ";
            });

    /**
     * 接收两个参数返回一个参数
     */
    static BiFunction<String, Integer, Long> bif = ((BiFunction) (s, i) -> 0l).andThen(t -> {
        System.err.println(t);
        return t + "then -> ";
    });

    /**
     * 消费者
     * 接收一个参数,然后执行方法体,没有返回值.
     */
    static Consumer<String>            c        = (d)       -> System.err.println("Consumer        -> " + d);
    static BiConsumer<String, Integer> bc       = (b, i)    -> System.err.println("BiConsumer      -> " + b + "-" + i);
    static DoubleConsumer              bbc      = b         -> System.err.println("DoubleConsumer  -> " + b);
    static IntConsumer                 ic       = b         -> System.err.println("IntConsumer     -> " + b);
    static LongConsumer                lc       = b         -> System.err.println("LongConsumer    -> " + b);
    static MyConsumer          mc =    (p1,p2,p3,p4,p5)     -> System.err.println("MyConsumer      -> " + Stream.of(p1,p2,p3,p4,p5).map(String::valueOf).collect(Collectors.joining(",")));

    /**
     * 谓词
     * 接收一个参数,返回true or false.
     * 就像汉语中的谓词,
     * 例如:
     * "猫是动物"一句中的"是"就是一个谓词,而"猫"是客体。
     * cat -> animal.containsKey(cat);
     * <p>
     * "3 大于 2"中"大于"是一个谓词。
     * num -> 3>2;
     * <p>
     * 3+1等于5
     * <p>
     * num -> 3+1 == 5;
     */
    static Predicate<String> p = (d) -> true;
    static BiPredicate<String, Integer> bp = (s, i) -> true;


    /**
     * 一元运算符
     */
    static UnaryOperator up = d -> d;
    /**
     * 二元运算符
     */
    static BinaryOperator<String> bo = (s, v) -> v;
    static BinaryOperator<String> minBo = BinaryOperator.minBy(Comparator.reverseOrder());
    static BinaryOperator<String> maxBo = BinaryOperator.maxBy(Comparator.reverseOrder());

    static DoubleBinaryOperator dbo = (s, v) -> v;
    static IntBinaryOperator ibo = (s, v) -> v;
    static LongBinaryOperator lbo = (s, v) -> v;

    /**
     * 布尔值供应商
     * 不接受参数,在主体进行操作返回true or false.
     */
    static Supplier<Object> s = () -> new Object();
    static BooleanSupplier  bs = () -> true;
    static IntSupplier      is = () -> 1;
    static DoubleSupplier   ds = () -> 1d;
    static LongSupplier     ls = () -> 1l;


    /**
     * 比较大小
     * v1 大于 v2返回true
     *
     * @param args
     */
    static BiPredicate<String, String> compare = (v1, v2) -> ((Comparator<String>) Comparator.naturalOrder()).compare(v1, v2) > 0;


    public static void main(String[] args) {

        System.err.println("--------------------Function");
        f.apply("start ");


        System.err.println("--------------------Predicate");
        System.err.println("Predicate   -> " + p.test("1"));
        System.err.println("BiPredicate -> " + bp.test("1",2));


        System.err.println("--------------------UnaryOperator");
        System.err.println(up.apply("1"));


        System.err.println("--------------------BinaryOperator");
        System.err.println("BinaryOperator        -> " + bo.apply("1", "2"));
        System.err.println("minBy                 -> " + minBo.apply("111", "2"));
        System.err.println("maxBy                 -> " + maxBo.apply("111", "2"));
        System.err.println("DoubleBinaryOperator  -> " + dbo.applyAsDouble(1d, 2d));
        System.err.println("IntegerBinaryOperator -> " + ibo.applyAsInt(1, 2));
        System.err.println("LongBinaryOperator    -> " + lbo.applyAsLong(1l, 2l));


        System.err.println("--------------------Supplier");
        System.err.println("Supplier          -> " + s.get());
        System.err.println("BooleanSupplier   -> " + bs.getAsBoolean());
        System.err.println("IntSupplier       -> " + is.getAsInt());
        System.err.println("DoubleSupplier    -> " + ds.getAsDouble());
        System.err.println("LongSupplier      -> " + ls.getAsLong());


        System.err.println("--------------------Consumer");
        c.accept("1");
        bc.accept("1", 2);
        bbc.accept(1d);
        ic.accept(1);
        lc.accept(1l);
        mc.accept(1,2,3,4,5);
        MyConsumer.print().accept("One", "two", "three", "four", "five");



        System.err.println("--------------------compare");
        System.err.println(compare.test("8", "7"));
    }

}

--------------------Function
start 
start compose1 -> 
start compose1 -> compose -> 
start compose1 -> compose -> then -> 
--------------------Predicate
Predicate   -> true
BiPredicate -> true
--------------------UnaryOperator
1
--------------------BinaryOperator
BinaryOperator        -> 2
minBy                 -> 2
maxBy                 -> 111
DoubleBinaryOperator  -> 2.0
IntegerBinaryOperator -> 2
LongBinaryOperator    -> 2
--------------------Supplier
Supplier          -> java.lang.Object@3532ec19
BooleanSupplier   -> true
IntSupplier       -> 1
DoubleSupplier    -> 1.0
LongSupplier      -> 1
--------------------Consumer
Consumer        -> 1
BiConsumer      -> 1-2
DoubleConsumer  -> 1.0
IntConsumer     -> 1
LongConsumer    -> 1
MyConsumer      -> 1,2,3,4,5
MyConsumer print -> One,two,three,four,five
--------------------compare
true

Process finished with exit code 0

上一篇 下一篇

猜你喜欢

热点阅读