美文网首页
Java8 函数式接口扫盲

Java8 函数式接口扫盲

作者: WebGis学习笔记 | 来源:发表于2021-07-02 10:14 被阅读0次

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

相关文章

  • JAVA8函数式接口学习

    JAVA8函数式接口 函数式接口是java8的一种新特性,函数式接口定义了且只定义了一个抽象方法!该接口非常有用,...

  • Java8 函数式接口扫盲

  • 函数式接口

    函数式接口 声明:java8新特性系列为个人学习笔记,参考地址点击这里,侵删!! 函数式接口(Functional...

  • java8系列-02 函数式接口(Function、Consum

    上一章已经说了函数式接口的基本概念(java8系列-01 Lambdas 表达式)。函数式接口(Functiona...

  • JAVA8新特性之函数式接口使用与学习

    函数式接口 函数式接口是Java8引用的一个新特性,是一种特殊的接口 SAM类型的接口(Single Abstra...

  • Java8值函数式接口

    Java8值函数式接口 如果一个接口中,只声明了一个抽象方法,则此接口就称为函数式接口 在接口上声明@Functi...

  • java8 lambda

    java8 lambda 函数式接口 谓词 Predicate boolean test(T t); T -...

  • Lambda表达式

    Lambda表达式是Java8的一个新特性,是函数式接口的一种体现。所谓函数式接口(functional inte...

  • 函数式接口(二)

    前面的文章中说明了Java8中最基本的4种函数式接口,我们今天先介绍其它Cosumer函数式接口。 BiConsu...

  • Java 8 新特性 - 函数式接口 Functional In

    Java8的其中一个新特性,函数式接口。 什么是函数式接口?有且仅有一个抽象方法的接口(不包括默认方法、静态方法以...

网友评论

      本文标题:Java8 函数式接口扫盲

      本文链接:https://www.haomeiwen.com/subject/izseultx.html