Java8 new Features (ii)----Functional Interface (functional) __linux

Source: Internet
Author: User

The previous blog introduced Java8 's new lambda expression, and this section describes JAVA8 's functional programming, and what is the connection between the two. Please look down ~ ~ ~

How the lambda expression is represented in the Java type.

Language designers put a lot of effort into thinking about how to make existing functions to support lambda in a friendly way. The ultimate approach is to increase the concept of functional interface, a functional interface is a common interface with a method. Like this.

An interface can be implicitly converted to a lambda expression ...

Each lambda expression corresponds to a type, usually the interface type. A "functional interface" refers to an interface that contains only an abstract method, and each lambda expression of that type is matched to this abstract method.

Because the default method is not an abstract method, you can also add the default method to your functional interface ....

We can use lambda expressions as an interface type for any abstract method, and your interface meets this requirement, you only need to add @functionalinterface annotations to your interface, and the compiler will complain if it finds that you have more than one interface for the annotation.

Take a look at the following functional programming examples:

Package com.panther.dong.java8.functional;

/**
 * Created by Panther on 15-8-22.
 *
/public class FunctionTest {public
    static void Main (string[] args) {
        converter<string, integer> converter = (from)-> integer.parseint (from);
        Integer integer = Converter.convert ("231");
        System.out.println (integer);
    }

@FunctionalInterface
Interface Converter<f, t> {
    T convert (F from);
}

Run Result:

231

Note: The default and Static methods in an interface do not affect the requirements of functional interfaces and can be used arbitrarily, but common methods can only have one

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.