Java8 lambda-functioanl Interface-function interface

Source: Internet
Author: User

The concept of functional interface is added to Java 8. And added a new annotation @FunctionalInterfaceI


1 What is the functional interface function interface?

Functional Interface:functional interface only has 1 abstract method. This is the definition of the function interface. Simply understood, the function interface is actually an interface of an abstract method (interface).


2 Types of functional interfaces

Here I only introduce a few types of basic predicate<t>,consumer<t>,function<t, r>,supplier<t>

These functions are declared in the Java.util.function package, you can see the code on their own.


1 predicate<t>--receives the T (generics) Generic object and returns a Boolean

Boolean Test (T T);

Often used for judgment, e.g. Stream.filter (predicate<t>)


2 consumer<t>--receive Tgenerics) generic object, no return value

void Accept (T t)

Used as Consumer such as foreach (consumer<t>)


3 Function<t, r>--receive tgenerics) generic object, return R (generics) Generic Object

T-R

R apply (T t)

Example Stream.map (function<t, r>)


4 supplier<t>--provides tgenerics) generic objects (such as factories) that do not receive values

() T



3. Attention to Detail

The function interface does not allow only one method, because Java 8 adds a new default method. Therefore, a functional interface can also have multiple default methods. However, a functional interface can have only one abstract method.

This article is from the "Development" blog, make sure to keep this source http://jamesdev.blog.51cto.com/2066624/1858763

Java8 lambda-functioanl Interface-function 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.