(原)

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:

  1. package com.demo.jdk8;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.function.Consumer;
  6.  
  7. public class Test2 {
  8. public static void main(String[] args) {
  9. for_test();
  10. for_newMethod();
  11. for_lambda();
  12. }
  13.  
  14. public static void for_test(){
  15. List<Integer> list = Arrays.asList(1,2,3,4,5,6);
  16.  
  17. for(Integer i : list){
  18. System.out.println(i);
  19. }
  20. }
  21.  
  22. public static void for_newMethod(){
  23. List<Integer> list = Arrays.asList(1,2,3,4,5,6);
  24.  
  25. list.forEach(new Consumer<Integer>() {
  26.  
  27. @Override
  28. public void accept(Integer t) {
  29. System.out.println(t);
  30. }
  31.  
  32. });
  33. }
  34.  
  35. public static void for_lambda(){
  36. List<Integer> list = Arrays.asList(1,2,3,4,5,6);
  37. list.forEach(i -> System.out.println(i));
  38. }
  39. }

  

这三个方法最后执行后的结果都一样

在for_newMethod方法中,可以看到list新增了一个新方法,forEach,可以迭代里面的元素,它的参数是一个consumer接口。

  1. Consumer接口在java.util.function包下,该包是java8新引入的工具包,该接口上有一个@FunctionalInterface注解。
  2. /*
  3. * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
  4. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  5. */
  6.  
  7. package java.lang;
  8.  
  9. import java.lang.annotation.*;
  10.  
  11. /**
  12. * An informative annotation type used to indicate that an interface
  13. * type declaration is intended to be a <i>functional interface</i> as
  14. * defined by the Java Language Specification.
  15. *这是一个通知性的注解类型,用于去表示某一个接口声明,它指在规定一个函数式接口,
  16. *这个接口由JAVA语言规范去定义的。(也就是说如果一个接口被加上@FunctionalInterface,
  17. *则表示这个接口是一个函数式接口)
  18. * Conceptually, a functional interface has exactly one abstract
  19. * method. Since {@linkplain java.lang.reflect.Method#isDefault()
  20. * default methods} have an implementation, they are not abstract. If
  21. * an interface declares an abstract method overriding one of the
  22. * public methods of {@code java.lang.Object}, that also does
  23. * <em>not</em> count toward the interface's abstract method count
  24. * since any implementation of the interface will have an
  25. * implementation from {@code java.lang.Object} or elsewhere.
  26. *从概念上来说,一个函数式接口只有一个抽象方法,由于java.lang.reflect.Method的方法sDefault()有一个实现,它们不是抽象的。如果一个接口声明了一个接口的方法,重写了java.lang.Object类里面的一个public方法,那么它也没有计入接口的抽象方法加1,
  27. *因为接口的任意一个实现都会有一个来自于java.lang.Object类或其它地方的一个实现。
  28. * <p>Note that instances of functional interfaces can be created with
  29. * lambda expressions, method references, or constructor references.
  30. *注意,含数式接口的实例可以通过lambda表达式、方法引用、或者构造方法引用来创建。
  31. * <p>If a type is annotated with this annotation type, compilers are
  32. * required to generate an error message unless:
  33. *如果一个注解类型带上了这个@FunctionalInterface,编译器会生成一个错误消息,除非以下几种情况:
  34. * <ul>
  35. * <li> The type is an interface type and not an annotation type, enum, or class.
  36. * <li> The annotated type satisfies the requirements of a functional interface.
  37. * </ul>
  38. *这个类型是一个接口类型,并且不是注解、枚举或class类型
  39. *被注解的类型满足了函数式接口的要求
  40. * <p>However, the compiler will treat any interface meeting the
  41. * definition of a functional interface as a functional interface
  42. * regardless of whether or not a {@code FunctionalInterface}
  43. * annotation is present on the interface declaration.
  44. *然而,编译器将会对待满足函数式接口定义的任义的接口,都会把它当成是一个函数式接口而不管是否在它的声明上增加了@FunctionalInterface注解类型。
  45. * @jls 4.3.2. The Class Object
  46. * @jls 9.8 Functional Interfaces
  47. * @jls 9.4.3 Interface Method Body
  48. * @since 1.8
  49. */
  50. @Documented
  51. @Retention(RetentionPolicy.RUNTIME)
  52. @Target(ElementType.TYPE)
  53. 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) -> {代码块} 例1: package com.demo.jdk8; i ...

随机推荐

  1. [nodejs] nodejs开发个人博客(四)数据模型

    数据库模型 /model/db.js 数据库操作类,完成链接数据库和数据库的增删查改 查询表 /*查询*/ select:function(tableName,callback,where,field ...

  2. Python 字典(Dictionary) 基本操作

    Python字典是一种可变容器模型,可存储任意类型对象:如字符串.数字.元组等.它以键值对(key-value)的形式存在,因此相当于Hashmap在python中的实现. §1. 创建字典  字典由 ...

  3. Spring Cloud 研发框架demo

    第一步:准备工作 1.下载并集成公司自定义maven maven包见QQ群文件 2.克隆Git源码到本地eclipse: xx 3.构建项目 一键初始化parent:run as maven inst ...

  4. 7个拒绝使用TypeScript的借口

    译者按: TypeScript 学习成本不高,项目切换成本不低,不过还是值得试一试的! 原文:7 bad excuses for not using TypeScript 译者: Fundebug 为 ...

  5. 洛谷P3379 【模板】最近公共祖先(LCA)(树链剖分)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  6. python中经典类和新式类的区别

    要知道经典类和新式类的区别,首先要掌握类的继承.类的继承的一个优点就是减少代码,而且使代码看起来结构很完整. 那什么是经典类,什么是新式类呢? 经典类和新式类的主要区别就是类的继承的方式 ,经典类遵循 ...

  7. Windows下git设置代理服务器

    SVN中,使用TortoiseSVN来进行版本控制时,设置代理非常简单,只需要在设置里面添加代理的信息即可.而 git 在GUI(v0.17.GITGUI)中却无法找到类似的设置,只能求助 git b ...

  8. java程序存入数据库中文乱码解决方案

    一.问题描述 背景:代码迁移,ssm框架在插入数据到mysql数据库时,中文乱码.代码中的编码配置没有问题,因为该项目代码以前使用过,没有问题.现在换了数据库,数据库配置也做了修改,统一使用utf8, ...

  9. 在Arcmap中加载互联网地图资源的4种方法

    前一段时间想在Arcmap中打开互联网地图中的地图数据,如影像数据.基础地图数据等,经过简单研究目前总结了四种方法,整理下与大家分享,有些内容可能理解有误,希望大家多多指教.4种方法如下: a)    ...

  10. Deep Learning - 2 反向传播

    深度神经网络的学习基于两个关键技术: Stochastic Gradient Descent Backpropagation 利用 SGD 算法学习 Weights 和 Biases,利用 Backp ...