Can a java lambda have more than 1 parameter?

后端 未结 6 2069
渐次进展
渐次进展 2020-11-28 18:24

In Java, is it possible to have a lambda accept multiple different types?

I.e: Single variable works:

    Function  adder = i         


        
6条回答
  •  悲&欢浪女
    2020-11-28 19:01

    To make the use of lambda : There are three type of operation:
    1. Accept parameter --> Consumer
    2. Test parameter return boolean --> Predicate
    3. Manipulate parameter and return value --> Function

    Java Functional interface upto two parameter:
    Single parameter interface
    Consumer
    Predicate
    Function

    Two parameter interface
    BiConsumer
    BiPredicate
    BiFunction

    For more than two, you have to create functional interface as follow(Consumer type):

    @FunctionalInterface
    public interface FiveParameterConsumer {
        public void accept(T t, U u, V v, W w, X x);
    }
    

提交回复
热议问题