Java 8 new Features 1-function interface

Source: Internet
Author: User

Java 8 new features 1-function interface Original

LAMBDA Expression Basic structure:

(PARAM1,PARAM2,PARAM3), { code block }

Example 1:

Package Com.demo.jdk8;import Java.util.arrays;import Java.util.list;import Java.util.function.consumer;public class Test2 {public static void main (string[] args) {for_test (); For_newmethod (); For_lambda ();} public static void For_test () {list<integer> List = Arrays.aslist (1,2,3,4,5,6), for (Integer i:list) { System.out.println (i);}} public static void For_newmethod () {list<integer> List = Arrays.aslist (1,2,3,4,5,6); List.foreach (New Consumer <Integer> () {@Overridepublic void accept (Integer t) {System.out.println (t);}});} public static void For_lambda () {list<integer> List = Arrays.aslist (1,2,3,4,5,6); List.foreach (i- System.out.println (i));}}

  

The results of these three methods are the same after the last execution

In the For_newmethod method, you can see that the list has added a new method,forEach , to iterate over the elements, whose parameters are a consumer interface.

Consumer interface under the Java.util.function package, the package is a newly introduced toolkit java8, which has a @functionalinterface interface. /* Copyright (c), Oracle and/or its affiliates. All rights reserved. * ORACLE proprietary/confidential. Use are subject to license terms. */package Java.lang;import java.lang.annotation.*;/** * An informative annotation type used to indicate this an interface * Type declaration is intended to being a <i>functional interface</i> as * defined by the Java Language Specifica Tion.* This is a notification annotation type that is used to represent an interface declaration, which refers to a functional interface, which is defined by the Java language Specification. (That is, if an interface is added @functionalinterface,* the interface is a functional interface) * Conceptually, a functional interface has exactly one abstract * me  Thod.  Since {@linkplain java.lang.reflect.method#isdefault () * Default Methods} has an implementation, they is not abstract. IF * An interface declares a abstract method overriding one of the * public methods of {@code Java.lang.Object}, that Al So does * <em>not</em> count toward the interface ' s abstract method Count *Since any implementation of the interface would have a * implementation from {@code Java.lang.Object} or elsewhere. * Conceptually, a functional interface has only one abstract method, because the Java.lang.reflect.Method method Sdefault () has an implementation, and they are not abstract. If an interface declares a method of an interface and overrides a public method inside the Java.lang.Object class, then it does not count to the abstract method of the interface plus 1, * Because any implementation of an interface will have an implementation from the Java.lang.Object class or somewhere else. * <p>note that instances of functional interfaces can is created with * lambda expressions, method references, or CO Nstructor references. * Note that an instance of a digital interface can be created through a lambda expression, a method reference, or a construction method reference.  * <p>if a type is annotated with this annotation type, compilers be * required to generate an error message unless:  * If an annotation type is taken with this @functionalinterface, the compiler generates an error message unless the following are true: * <ul> * <li> The type is a interface type and not An annotation type, enum, or class. * <li> The annotated type satisfies the requirements of a functional interface.  * </ul> * This type is an interface type and is not an annotation, enumeration, or class type * The annotated type satisfies the requirements of the functional interface * <p>however, the compiler would treat any interface Meeting the * Definition of a functional interface as a functional interface * regardless of whether or not a {@code functionalinterf ACE} * Annotation is present on the interface declaration. * However, the compiler will treat Khur interfaces that satisfy the definition of a functional interface, and it will be treated as a functional interface regardless of whether the @functionalinterface annotation type is added to its declaration. * @jls 4.3.2. The Class Object * @jls 9.8 functional Interfaces * @jls 9.4.3 Interface Method Body * @since 1.8 */@Documented @retention ( Retentionpolicy.runtime) @Target (elementtype.type) public @interface Functionalinterface {}

put @FunctionalInterface (functional interface) of the document translated, or feel very around, and then summed up,

What is a functional interface:

1, the interface is added @FunctionalInterface, indicating that it is a functional interface.

2, if an interface has only one abstract method, then it is a functional interface.

3.If an interface has an abstract method and some method that overrides the public method of the Java.lang.Object class, it means that it is a functional interface.

Java 8 new Features 1-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.