New Features of Java 8-functional interfaces and relationships with Lambda expressions

Source: Internet
Author: User

New Features of Java 8-functional interfaces and relationships with Lambda expressions

Here we will explain the functional interfaces in the new features of Java 8 and Their Relationships with Lambda expressions. I have seen many articles about Java 8 and will introduce functional interfaces and lambda expressions, but they are both described separately. The relationship between them is not clearly described. Here, sort out your understanding as follows:

 

I. functional interfaces:

A functional interface is actually an interface, but it is a special interface: a SAM-type interface (Single Abstract Method ). This type of interface is defined so that it can be used as a parameter. A lambda expression can be used as a parameter during the call. On the other hand, once we call a method and pass in a lambda expression as a parameter, the parameter type of this method must be a functional interface, this type must be modified using @ FunctionalInterface.

In principle, SAM can only implement one function in this interface, but the following exceptions can also be found:

1. the default method and static method do not affect the contract of the function interface. They can be used at will, that is

Function interfaces can contain static methods. One or more static methods do not affect the SAM interface as a function interface, and static methods can provide method implementation.

The default method that can be modified by default. This keyword is added in Java 8 to implement some interfaces. In principle, only one method is implemented, but for historical reasons, you have to add some methods to be compatible with the entire jdk api, so you need to use the default keyword to define this method.

2. There can be methods covered in objects, such as equals, toString, and hashcode.

In JDK, all previous functional interfaces have been defined using @ FunctionalInterface. You can check the JDK source code to confirm that the existing functional interfaces before JDK 8 are attached as follows:

Java. lang. Runnable

Java. util. concurrent. Callable

Java. security. PrivilegedAction

Java. util. Comparator

Java. io. FileFilter

Java. nio. file. PathMatcher

Java. lang. reflect. InvocationHandler

Java. beans. PropertyChangeListener

Java. awt. event. ActionListener

Javax. swing. event. ChangeListener

For example:

@FunctionalInterfacepublic interface Runnable {    /**     * When an object implementing interface <code>Runnable</code> is used     * to create a thread, starting the thread causes the object's     * <code>run</code> method to be called in that separately executing     * thread.     * <p>     * The general contract of the method <code>run</code> is that it may     * take any action whatsoever.     *     * @see     java.lang.Thread#run()     */    public abstract void run();}

The following is an example of a UDF interface:

Definition:

@FunctionalInterfaceinterface Converter<F, T> {    T convert(F from);}

Usage:

Converter<String, Integer> converter = (from) -> Integer.valueOf(from);Integer converted = converter.convert("123");

Note: Methods and constructor references can be called using the: Operator in Java 8.

 

In a self-designed method, if you can receive lambda expressions, you can use Function as a parameter. The following are some implemented functional interfaces:

// Function <T, R>-T is used as the input, and the returned R is used as the output Function <String, String> function = (x)-> {System. out. print (x + ":"); return "Function" ;}; System. out. println (function. apply ("hello world"); // Predicate <T>-T as the input, and the returned boolean value as the output Predicate <String> pre = (x)-> {System. out. print (x); return false ;}; System. out. println (":" + pre. test ("hello World"); // Consumer <T>-T is used as the input, and a certain action is executed, but no return value Consumer <String> con = (x)-> {System. out. println (x) ;}; con. accept ("hello world"); // Supplier <T>-returns T Supplier <String> supp = ()-> {return "Supplier ";}; system. out. println (supp. get (); // BinaryOperator <T>-two TBS are used as input, and one T is returned as output. BinaryOperator <String> bina = (x, y)-> {System. out. print (x + "" + y); return "BinaryOperator" ;}; System. out. println ("" + bina. apply ("hello", "world "));

 

Ii. Lambda expressions (here is a simple introduction)

Writing Method: e-> System. out. println (e)

1. Three parts

Parameter List

Symbol>

Function body: contains multiple statements, which can be included by {}. If you need to return a value and only one statement, you can omit return.

2. Access Control:

It is possible to convert the member variables and local variables of the category class (non-final will be automatically converted to final)

 

The above is a brief introduction to functional interfaces. If you have any omissions, please correct them.

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.