Lambda expression of java8 (functional interface)

Source: Internet
Author: User

There are many existing interfaces in Java that need to encapsulate code blocks, such as: runnable or comparator. Lambda expressions are backwards-compatible with these interfaces. For an interface that contains only an abstract method, you can create an object of that interface through a lambda expression, which is called a functional interface. Note:interfaces in JAVA8 can declare non-abstract methods .

To demonstrate functional interface transformations, we take the Arrays.sort method as an example. The second parameter of the method requires an instance of the comparator interface (which contains only one method). Next we write a simple lambda expression:

Arrays.sort (words, (First,second), Integer.compare (First.length (), Second.length ()));

Behind this expression, the Arrays.sort method receives an instance of the class that implements the Comparaotr<string> interface. Calling the object's compare method executes the code in the lambda expression. The management of these objects and classes relies entirely on how they are implemented, and is therefore more efficient than traditional internal classes. You'd better think of a lambda expression as a function, not an object, and remember that it can be converted to a functional interface.

In fact, the conversion of a functional interface is the only thing you can do with a lambda expression in Java. In other programming languages that support function text, you can declare function types such as int (string,string), declare variables of this type, and use these variables to hold function expressions. However, Java designers have decided to stick to familiar interface concepts instead of adding function types to java.

The Java API defines a number of very general functional interfaces in the Java.util.function package (which is explained in detail later in the blog). Where interface bifunction<t,u,r> describes the method parameters of the T and U types and the return type R. You can save our string comparison lambda expression in a variable of that type:

Bifunction<string,string,integer> comp = (first,second), Integer.compare (First.length (), Second.length ()) ;

However, this is not helpful for sorting. There is no Arrays.sort method to receive bifunction as a parameter. If you've used other functional programming languages before, you might be surprised. But for Java developers, this is no more natural. Interfaces like comparator have a specific purpose, not just a method of receiving parameters and return types. Java8 retained the habit. When you want to use a lambda expression, you still have to remember the purpose of the expression and assign it a functional interface.

Now the API of JAVA8 itself uses the interfaces in the java.util.function, which are likely to be used in the future. Keep in mind, however, that any lambda expression can be converted to the corresponding functional interface in the API that is currently in use.

Note: You can annotate @functionalinterface annotations on any functional interface, which has two benefits. First, the compiler checks the entity that labels the annotation and checks to see if it is an interface that contains only an abstract method. In addition, the Javadoc page also contains a declaration stating that the interface is a function-type interface. This annotation does not require mandatory use. Conceptually, all interfaces that contain only one abstract method are functional interfaces, but using @functionalinterface annotations will make your code look clearer.

Finally, when a lambda expression is converted to an instance of a functional interface, be aware of handling check-period exceptions. If a check-period exception may be thrown in a lambda expression, the exception needs to be declared in the abstract method of the target interface. For example, the following expression produces an error:

Error: Thread.Sleep can throw a check period of interruptedexceptionrunnable sleeper = (), {System.out.println ("Zzz"); Thread.Sleep (1000);};

Since Runnable.run cannot throw any exceptions, this assignment is not legal. There are two ways to fix the problem. One is to catch an exception in a lambda expression, and the other is to assign a lambda expression to an interface whose abstract method can throw an exception. For example, the call method of the callable interface can throw any exception.





Lambda expression of java8 (functional interface)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.