Functional Interface

Functional Interface是什么?

功能接口是java 8中的新增功能,它们只允许一个抽象方法。这些接口也称为单抽象方法接口(SAM接口)。这些也可以使用Lambda表达式,方法引用和构造函数引用来表示。Java 8也引入了一个注释,即@FunctionalInterface,当你注释的接口违反了Functional Interface的契约时,它可以用于编译器级错误。

Tips:如果你想强制当前接口只保留一个抽象方法,就可以使用@FunctionalInterface,它可以帮我们通过编译错误提示用户必须不能声明超过一个抽象方法

构建我们第一个默认接口:

  1. @FunctionalInterface
  2. public interface MyFuntinoalInterface<T> {
  3. public void eat();
  4. /**
  5. * 超过一个抽象方法会编译报错,可以把下面注释去掉查看效果
  6. */
  7. //public void play();
  8. }

尝试将上例的注释去掉,可以发现,超过两个抽象方法会直接报编译错误

  1. @FunctionalInterface
  2. public interface MyFuntinoalInterface<T> {
  3. public void eat();
  4. /**
  5. * 超过一个抽象方法会编译报错,可以把下面注释去掉查看效果
  6. */
  7. public void play();
  8. }

报如下编译异常:

功能接口的使用原则

  • 在任何功能接口中只允许一种抽象方法。功能界面中不允许使用第二种抽象方法。如果我们删除@FunctionInterface注释,那么我们可以添加另一个抽象方法,但它会使接口成为非功能性接口。
  • 即使省略@FunctionalInterface注释,功能接口也是有效的。它仅用于通知编译器在接口内强制执行单个抽象方法。
  • 从概念上讲,功能界面只有一种抽象方法。由于默认方法具有实现,因此它们不是抽象的。由于默认方法不是抽象的,因此您可以根据需要随意添加默认方法到功能接口。

你可以添加多个默认方法到功能接口中,但是它不会计入抽象方法的数量:

  1. @FunctionalInterface
  2. public interface MyFuntinoalInterface<T> {
  3. public void eat();
  4. default void run(){
  5. System.out.println("跑步");
  6. }
  7. default void swim(){
  8. System.out.println("游泳");
  9. }
  10. }
  • 功能接口里可以有多个覆盖java.lang.Objectd的抽象方法,它不会计入接口的抽象方法数量。
  1. @FunctionalInterface
  2. public interface MyFuntinoalInterface<T> {
  3. public void eat();
  4. /**
  5. * 重写Object方法不会算入抽象方法统计
  6. * @return
  7. */
  8. @Override
  9. public String toString();
  10. }

JDK 中的功能接口

JDK1.8开始支持功能接口,以下可以举几个例子,功能接口主要是配合lambda表达式使用,具体介绍会在后续的lambda表达式章节也会提到

Comparator

  1. @FunctionalInterface
  2. public interface Comparator<T>

在JDK1.8中,主要对Comparator接口添加了默认方法,对Comparator实现类添加了很多新特性方法

Consumer

  1. @FunctionalInterface
  2. public interface Consumer<T>

相信大家查看JDK1.8源码会发现,很多类都有Consumer这个接口存在,在JDK1.8中更多是提供各个接口支持lambda表达式使用

Iterable

  1. public interface Iterable<T> {
  2. /**
  3. * Returns an iterator over elements of type {@code T}.
  4. *
  5. * @return an Iterator.
  6. */
  7. Iterator<T> iterator();
  8. /**
  9. * Performs the given action for each element of the {@code Iterable}
  10. * until all elements have been processed or the action throws an
  11. * exception. Unless otherwise specified by the implementing class,
  12. * actions are performed in the order of iteration (if an iteration order
  13. * is specified). Exceptions thrown by the action are relayed to the
  14. * caller.
  15. *
  16. * @implSpec
  17. * <p>The default implementation behaves as if:
  18. * <pre>{@code
  19. * for (T t : this)
  20. * action.accept(t);
  21. * }</pre>
  22. *
  23. * @param action The action to be performed for each element
  24. * @throws NullPointerException if the specified action is null
  25. * @since 1.8
  26. */
  27. default void forEach(Consumer<? super T> action) {
  28. Objects.requireNonNull(action);
  29. for (T t : this) {
  30. action.accept(t);
  31. }
  32. }
  33. /**
  34. * Creates a {@link Spliterator} over the elements described by this
  35. * {@code Iterable}.
  36. *
  37. * @implSpec
  38. * The default implementation creates an
  39. * <em><a href="Spliterator.html#binding">early-binding</a></em>
  40. * spliterator from the iterable's {@code Iterator}. The spliterator
  41. * inherits the <em>fail-fast</em> properties of the iterable's iterator.
  42. *
  43. * @implNote
  44. * The default implementation should usually be overridden. The
  45. * spliterator returned by the default implementation has poor splitting
  46. * capabilities, is unsized, and does not report any spliterator
  47. * characteristics. Implementing classes can nearly always provide a
  48. * better implementation.
  49. *
  50. * @return a {@code Spliterator} over the elements described by this
  51. * {@code Iterable}.
  52. * @since 1.8
  53. */
  54. default Spliterator<T> spliterator() {
  55. return Spliterators.spliteratorUnknownSize(iterator(), 0);
  56. }
  57. }

Iterable虽然接口头上没有@FunctionalInterface注解标注,它这里只有一个抽象方法,默认也是一个功能接口,在JDK1.8中新添加了支撑lambda表达式的默认方法

Java8新特性一点通 | 回顾功能接口Functional Interface的更多相关文章

  1. Java8新特性一点通 | 回顾字符转日期&JoinArray使用

    StringToDate日期转换 Join Array使用 StringToDate日期转换 Convert string to date in ISO8601 format 利用LocalDate. ...

  2. Java8新特性一点通 | 回顾文件操作和watchService文件变更监视操作

    文件操作 WatchService 文件操作 今天重温了一些文件操作: - Files.list() 遍历文件和目录 //List all files and sub-directories usin ...

  3. Java8新特性—四大内置函数式接口

    Java8新特性--四大内置函数式接口 预备知识 背景 Lambda 的设计者们为了让现有的功能与 Lambda 表达式良好兼容,考虑了很多方法,于是产生了函数接口这个概念. 什么是函数式接口? 函数 ...

  4. java8新特性—四大内置核心接口

    java8新特性-四大内置核心接口 四大内置核心接口 //消费型接口 Consumer<T>:: vode accept(T t); //供给型接口 Supplier<T>:: ...

  5. Java8新特性_lambda表达式和函数式接口最详细的介绍

    Lambda表达式 在说Lambda表达式之前我们了解一下函数式编程思想,在数学中,函数就是有输入量.输出量的一套计算方案,也就是“拿什么东西做什么事情”. 相对而言,面向对象过分强调“必须通过对象的 ...

  6. Java8新特性时间日期库DateTime API及示例

    Java8新特性的功能已经更新了不少篇幅了,今天重点讲解时间日期库中DateTime相关处理.同样的,如果你现在依旧在项目中使用传统Date.Calendar和SimpleDateFormat等API ...

  7. Java8新特性(一)——Lambda表达式与函数式接口

    一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...

  8. JAVA8新特性——接口定义增强

    JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ 接口定义增强 在JDK1.8以前,接口是定义的: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法 ...

  9. java8新特性学习:函数式接口

    本文概要 什么是函数式接口? 如何定义函数式接口? 常用的函数式接口 函数式接口语法注意事项 总结 1. 什么是函数式接口? 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口 ...

随机推荐

  1. c++ 屏幕截图指定窗口句柄后台截图返回位图句柄

    /根据窗口句柄后台截图保存成BMP位图文件并且显示到picture 控件上 void GetScreenBmp(HWND hwnd, int left, int top, int width, int ...

  2. SpringBoot拦截器没有生效的原因

    最近的一个项目中,需要实现一个记录系统请求日志的功能,我通过拦截器实现了这个功能,但是,测试的时候发现拦截器没有发挥作用,检查了很久,没有发现错的地方,系统也没有报错,最终在网上找打了答案. 原来之前 ...

  3. python+selenium+Chrome options参数

    python+selenium+Chrome options参数 Chrome Options常用的行为一般有以下几种: 禁止图片和视频的加载:提升网页加载速度. 添加代理:用于翻墙访问某些页面,或者 ...

  4. Shell one

    1.shell的运算符包括:算术运算符.关系运算符.布尔运算符.字符串运算符.文件测试运算符 1.1原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr ...

  5. C Primer Plus(二)

    重读C Primer Plus ,查漏补缺 重读C Primer Plus,记录遗漏的.未掌握的.不清楚的知识点 分支和跳转 1.ctype.h头文件里包含了一些列用于字符判断的函数,包括判断数字.大 ...

  6. 详解Spring IoC容器

    一.Spring IoC容器概述 1.依赖反转(依赖注入):依赖对象的获得被反转了. 如果合作对象的引用或依赖关系的管理由具体对象来完成,会导致代码的高度耦合和可测试性的降低,这对复杂的面向对象系统的 ...

  7. schedule of 2016-10-31~2016-11-6(Monday~Sunday)——1st semester of 2nd Grade

    most important things to do 1.joint phd preparations 2.journal paper to write 3.solid fundamental kn ...

  8. oracle-按年、月、周、日、时、分 分组查询统计数据,无数据补零(connect by)

    目的:统计一段时间内每年.每月.每周.每日.每时.每分数据,无数据时自动补零 思路:1. 生成给定时间段对应日期 2. 将原表中该时间段内的不为0的数据量统计出来 3. 用left join连接起来, ...

  9. Java面向对象之异常【一】

    目录 Java面向对象之异常[一] 异常的继承体系 Error Exception 异常是否受检 unchecked exceptions(不受检异常) checked exceptions(受检异常 ...

  10. 树形dp - 求树的直径

    随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可 ...