What are functional interfaces used for in Java 8?

前端 未结 10 2113
Happy的楠姐
Happy的楠姐 2020-11-22 09:08

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions.

Java 8 provides

相关标签:
10条回答
  • 2020-11-22 09:44

    Not at all. Lambda expressions are the one and only point of that annotation.

    0 讨论(0)
  • 2020-11-22 09:44

    You can use lambda in Java 8

    public static void main(String[] args) {
        tentimes(inputPrm - > System.out.println(inputPrm));
        //tentimes(System.out::println);  // You can also replace lambda with static method reference
    }
    
    public static void tentimes(Consumer myFunction) {
        for (int i = 0; i < 10; i++)
            myFunction.accept("hello");
    }
    

    For further info about Java Lambdas and FunctionalInterfaces

    0 讨论(0)
  • 2020-11-22 09:46

    @FunctionalInterface is a new annotation are released with Java 8 and provide target types for lambda expressions and it used on compilation time checking of your code.

    When you want to use it :

    1- Your interface must not have more than one abstract methods, otherwise compilation error will be given.

    1- Your interface Should be pure, which means functional interface is intended to be implemented by stateless classes, exmple of pure is Comparator interface because its not depend on the implementers state, in this case No compilation error will be given, but in many cases you will not be able to use lambda with this kind of interfaces

    The java.util.function package contains various general purpose functional interfaces such as Predicate, Consumer, Function, and Supplier.

    Also please note that you can use lambdas without this annotation.

    0 讨论(0)
  • 2020-11-22 09:50

    The documentation makes indeed a difference between the purpose

    An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.

    and the use case

    Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

    whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to “Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?”

    Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:

    JLS §9.8. Functional Interfaces:

    In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).

    So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).

    So in one sentence, no, there is no other use case for it in Java 8.

    0 讨论(0)
  • 2020-11-22 09:52

    As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.

    0 讨论(0)
  • 2020-11-22 09:54

    Functional Interface:

    • Introduced in Java 8
    • Interface that contains a "single abstract" method.

    Example 1:

       interface CalcArea {   // --functional interface
            double calcArea(double rad);
        }           
    

    Example 2:

    interface CalcGeometry { // --functional interface
        double calcArea(double rad);
        default double calcPeri(double rad) {
            return 0.0;
        }
    }       
    

    Example 3:

    interface CalcGeometry {  // -- not functional interface
        double calcArea(double rad);
        double calcPeri(double rad);
    }   
    

    Java8 annotation -- @FunctionalInterface

    • Annotation check that interface contains only one abstract method. If not, raise error.
    • Even though @FunctionalInterface missing, it is still functional interface (if having single abstract method). The annotation helps avoid mistakes.
    • Functional interface may have additional static & default methods.
    • e.g. Iterable<>, Comparable<>, Comparator<>.

    Applications of Functional Interface:

    • Method references
    • Lambda Expression
    • Constructor references

    To learn functional interfaces, learn first default methods in interface, and after learning functional interface, it will be easy to you to understand method reference and lambda expression

    0 讨论(0)
提交回复
热议问题