Java 8 新特性1-函數式介面

來源:互聯網
上載者:User

標籤:pack   表達   form   instance   required   函數   總結   oracle   abs   

Java 8 新特性1-函數式介面(原)

Lambda運算式基本結構:

(param1,param2,param3) -> {代碼塊}

 

例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));}}

 

  

這三個方法最後執行後的結果都一樣

在for_newMethod方法中,可以看到list新增了一個新方法,forEach,可以迭代裡面的元素,它的參數是一個consumer介面。

 

 

Consumer介面在java.util.function包下,該包是java8新引入的工具包,該介面上有一個@FunctionalInterface介面。/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.lang;import java.lang.annotation.*;/** * An informative annotation type used to indicate that an interface * type declaration is intended to be a <i>functional interface</i> as * defined by the Java Language Specification.*這是一個通知性的註解類型,用於去表示某一個介面聲明,它指在規定一個函數式介面,     *這個介面由JAVA語言規範去定義的。(也就是說如果一個介面被加上@FunctionalInterface,*則表示這個介面是一個函數式介面) * Conceptually, a functional interface has exactly one abstract * method.  Since {@linkplain java.lang.reflect.Method#isDefault() * default methods} have an implementation, they are not abstract.  If * an interface declares an abstract method overriding one of the * public methods of {@code java.lang.Object}, that also does * <em>not</em> count toward the interface‘s abstract method count * since any implementation of the interface will have an * implementation from {@code java.lang.Object} or elsewhere. *從概念上來說,一個函數式介面只有一個抽象方法,由於java.lang.reflect.Method的方法sDefault()有一個實現,它們不是抽象的。如果一個介面聲明了一個介面的方法,重寫了java.lang.Object類裡面的一個public方法,那麼它也沒有計入介面的抽象方法加1, *因為介面的任意一個實現都會有一個來自於java.lang.Object類或其它地方的一個實現。 * <p>Note that instances of functional interfaces can be created with * lambda expressions, method references, or constructor references. *注意,含數式介面的執行個體可以通過lambda運算式、方法引用、或者構造方法引用來建立。 * <p>If a type is annotated with this annotation type, compilers are * required to generate an error message unless: *如果一個註解類型帶上了這個@FunctionalInterface,編譯器會產生一個錯誤訊息,除非以下幾種情況: * <ul> * <li> The type is an interface type and not an annotation type, enum, or class. * <li> The annotated type satisfies the requirements of a functional interface. * </ul> *這個類型是一個介面類型,並且不是註解、枚舉或class類型 *被註解的類型滿足了函數式介面的要求 * <p>However, the compiler will treat any interface meeting the * definition of a functional interface as a functional interface * regardless of whether or not a {@code FunctionalInterface} * annotation is present on the interface declaration. *然而,編譯器將會對待滿足函數式介面定義的任義的介面,都會把它當成是一個函數式介面而不管是否在它的聲明上增加了@FunctionalInterface註解類型。 * @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 {}

把@FunctionalInterface(函數式介面)的文檔翻譯了一下,還是感覺很繞,再此總結一下,

什麼是函數式介面:

1、介面被加上了@FunctionalInterface,表示它是一個函數式介面。

2、如果一個介面只有一個抽象方法,那麼表示它是一個函數式介面。

3、如果一個介面有一個抽象方法和一些重寫了java.lang.Object類公用方法的方法,那麼表示它是一個函數式介面。

 

Java 8 新特性1-函數式介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.