(原)

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

Lambda表达式基本结构:

(param1,param2,param3) -> {代码块}

Lambda表达式结构:

(type1 arg1,type2 arg2) -> {body}; //type1、type2表示参数类型,arg1、arg2表示参数,body表示方法体。

(arg1,arg2) -> (body;); //这种写法,编译器会自动判断出arg1,arg2的参数类型,也是正确的;

arg1 -> {body;}; //如果只有一个参数,参数的括号也可以省去。

arg1 -> {return arg1;}; //如果有返回值,可以这么写。

arg1 -> arg1; //输出值只有一句,方法体的大括号可以省掉。

例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新引入的工具包,该接口上有一个@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类公共方法的方法,那么表示它是一个函数式接口。

例子请看这里:https://github.com/LeeScofield/java8

Java 8 新特性1-函数式接口的更多相关文章

  1. Java 8新特性-1 函数式接口

    Java 8 引入的一个核心概念是函数式接口(Functional Interfaces). 通过在接口里面添加一个抽象方法,这些方法可以直接从接口中运行. 如果一个接口定义个唯一一个抽象方法,那么这 ...

  2. 乐字节-Java8新特性之函数式接口

    上一篇小乐带大家学过 Java8新特性-Lambda表达式,那什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口 ...

  3. Java(44)JDK新特性之函数式接口

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201667.html 博客主页:https://www.cnblogs.com/testero ...

  4. Java8新特性之函数式接口

    <Java 8 实战>学习笔记系列 定义 函数式接口只定义一个抽象方法,可以有多个默认方法 函数式接口的接口名上,会被@FunctionalInterface标注 作用 函数式接口的方法可 ...

  5. JDK8新特性:函数式接口@FunctionalInterface的使用说明

    我们常用的一些接口Callable.Runnable.Comparator等在JDK8中都添加了@FunctionalInterface注解. 通过JDK8源码javadoc,可以知道这个注解有以下特 ...

  6. jdk1.8新特性之函数式接口

    函数式接口就是只有一个抽象方法的接口.如果这个接口里没有或者包含了两个以上的抽象方法,对不起,你不叫函数式接口,只能叫你接口.那这个函数式有啥用呢?如果配合Lambda表达式的话,可以大大的简化代码. ...

  7. JDK8新特性:函数式接口

    一,定义 函数式接口,英文为Functional Interface.首先它是一个接口,那么它与其它接口有什么不同呢?不同点就是在这个接口中只允许有一个抽象方法. 这里的只允许一个抽象方法不包括以下几 ...

  8. JDK8新特性之函数式接口

    什么是函数式接口 先来看看传统的创建线程是怎么写的 Thread t1 = new Thread(new Runnable() { @Override public void run() { Syst ...

  9. Java 8 新特性:1-函数式接口

    (原) Java 8 新特性1-函数式接口 Lambda表达式基本结构: (param1,param2,param3) -> {代码块} Lambda表达式结构: (type1 arg1,typ ...

随机推荐

  1. APICloud框架—db数据库模块

    db数据库模块 db 模块封装了手机常用数据库 sqlite 的增删改查语句,可实现数据的本地存储,极大的简化了数据持久化问题,本模块已支持同步接口. 官方文档地址 打开/新建一个数据库 functi ...

  2. Spring+SpringMVC+MyBatis+easyUI整合优化篇(八)代码优化整理小记及个人吐槽

    日常啰嗦 这两天也一直在纠结这一篇文章该写什么东西,前面临时加的两篇文章就有些打乱了整体节奏,这一篇又想去写一下代码层面优化的事情,可是也不太能抓住重要的点,不太确定从何入手,因为这件事情牵涉了太多技 ...

  3. html 数字不转行问题

    代码如下 <div style="width:20px;height:20px"> 111111111111111111111111111111111111111111 ...

  4. 简谈-如何使用Python和R组合完成任务

    概述 和那些数据科学比赛不同,在真实的数据科学中,我们可能更多的时间不是在做算法的开发,而是对需求的定义和数据的治理.所以,如何更好的结合现实业务,让数据真正产生价值成了一个更有意义的话题. 数据科学 ...

  5. 【原创】bootstrap框架的学习 第五课

    一.Bootstrap 中定义了所有的 HTML 标题(h1 到 h6)的样式. <!DOCTYPE html> <html> <head> <title&g ...

  6. Mac os下安装brew

    1.首先没下载xcode,请先安装xcode,安装的继续往下面看 2.安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuser ...

  7. split()方法

    split()方法用于把一个字符串分隔成字符串数组. 它有两个参数: separator:从参数指定的地方分隔字符串,必需: howmany:该参数可指定返回的数组的最大长度.如果设置了该参数,返回的 ...

  8. 图零直播新闻发布会—TOLINK2.0全面上线

    在网络直播时代和现代信息技术条件下,教务管理正在由传统管理方式向数字化管理模式转变.教务管理创新需要现代信息技术来实现,使得教务管理的质量和效率得到了质的飞跃.图零直播,中国IT在线直播教育引领者,在 ...

  9. crontab的两种配置方式

    废话不多说,直接上菜了   第一种:在/etc/crontab下设置,指定用户名的 1.vim命令进入/etc/crontab 2.在最后一行加上 59 23 * * * root /root/cat ...

  10. web开发中,post与get的区别

    区别: 1.Get是从服务器上获取数据,Post是向服务器传送数据. 2.Get是把参数数据队列加到提交表单的Action属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.Post是 ...