1. 程式人生 > >Java 8 新特性1-函數式接口

Java 8 新特性1-函數式接口

實例 his sys subject 生成 license object類 acc class類

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>() {

			@Override
			public 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新引入的工具包,[email protected]
/*
 * 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語言規範去定義的。([email protected]
/* */, *則表示這個接口是一個函數式接口) * Conceptually, a functional interface has exactly one abstract * method. Since [email protected] 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 [email protected]
/* */ 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 [email protected] 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: [email protected],編譯器會生成一個錯誤消息,除非以下幾種情況: * <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 [email protected] FunctionalInterface} * annotation is present on the interface declaration. *然而,編譯器將會對待滿足函數式接口定義的任義的接口,都會把它當成是一個函數式接口而不管是否在它的[email protected] * @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-函數式接口