JDK8新特性之函数式接口

转载自 JDK8新特性之函数式接口

什么是函数式接口

先来看看传统的创建线程是怎么写的

Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {System.out.println("t1");}
});
t1.start();

再来看看使用了函数式接口是怎么写的

Thread t2 = new Thread(() -> System.out.println("t2"));
t2.start();

Runnable接口直接可以使用Lambda表达式来编写,这是因为Runnable接口是一个函数式接口,来看看Runnable的源码。

@FunctionalInterface
public interface Runnable {public abstract void run();}

发现该接口加上了函数式接口的定义注解: @FunctionalInterface,表明该接口是一个函数式接口。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}

在JDK8中,除了Runnbale接口,还有像Comparator、Callable等接口都加上了该注解定义为函数式接口。

内置函数式接口

JDK8提供了几个内置的函数式接口,用在了许多API的地方,都可以拿来用,可以满足大部分应用。

//Consumer<T> - T作为输入,执行某种动作但没有返回值
Consumer<String> con = (x) -> {System.out.println(x);
};
con.accept("hello world");//Supplier<T> - 没有任何输入,返回T
Supplier<String> supp = () -> {return "Supplier";
};
System.out.println(supp.get());//Predicate<T> -T作为输入,返回的boolean值作为输出
Predicate<String> pre = (x) -> {System.out.print(x);return x.startsWith("op");
};
System.out.println(": " + pre.test("op, hello World"));// Function<T, R> -T作为输入,返回的R作为输出
Function<String, String> function = (x) -> {System.out.print(x + ": ");return "Function";
};
System.out.println(function.apply("hello world"));//BinaryOperator<T> -两个T作为输入,返回一个T作为输出,对于“reduce”操作很有用
BinaryOperator<String> bina = (x, y) -> {System.out.print(x + " " + y);return "BinaryOperator";
};
System.out.println("  " + bina.apply("hello ", "world"));

自定义函数式接口

1、自定义一个函数式接口

@FunctionalInterface
public interface CalcInterface<N, V> {    V operation(N n1, N n2);
}

这里只有一个抽象方法,@FunctionalInterface注解可以不用写,至于为什么可以往下看。

2、新建一个引用函数式接口的类

public static class NumberOperation<N extends Number, V extends Number> {private N n1;private N n2;public NumberOperation(N n1, N n2) {this.n1 = n1;this.n2 = n2;}public V calc(CalcInterface<N, V> ci) {V v = ci.operation(n1, n2);return v;}}

3、测试函数式接口

private static void testOperationFnInterface() {NumberOperation<Integer, Integer> np = new NumberOperation(13, 10);CalcInterface<Integer, Integer> addOper1 = (n1, n2) -> {return n1 + n2;};CalcInterface<Integer, Integer> multiOper1 = (n1, n2) -> {return n1 * n2;};System.out.println(np.calc1(addOper1));System.out.println(np.calc1(multiOper1));// 上面的可以简写为System.out.println(np.calc1((n1, n2) -> n1 + n2));System.out.println(np.calc1((n1, n2) -> n1 * n2));
}

最后输出:

23
130
23
130

函数式接口规范

1、@FunctionalInterface标识为一个函数式接口只能用在只有一个抽象方法的接口上。

2、接口中的静态方法、默认方法、覆盖了Object类的方法都不算抽象方法。

3、@FunctionalInterface注解不是必须的,如果该接口只有一个抽象方法可以不写,它默认就符合函数式接口,但建议都写上该注解,编译器会检查该接口是否符合函数式接口的规范。

举例说明

正确的函数式接口。

@FunctionalInterface
public interface CalcInterface<N, V> {    V operation(N n1, N n2);
}

加了几个符合函数式的方法也没事,编译器也不会报错。

@FunctionalInterface
public interface CalcInterface<N, V> {        V operation(N n1, N n2);public boolean equals(Object object);public default void defaultMethod() {}public static void staticMethod() {}
}

这个没用@FunctionalInterface函数式接口,有两个抽象方法,不能用于Lambda表达式。

public interface CalcInterface<N, V> {    V operation(N n1, N n2);V operation2(N n1, N n2);
}

这个有两个抽象方法的用@FunctionalInterface注解的函数式接口编译会报错。

@FunctionalInterface
public interface CalcInterface<N, V> {    V operation(N n1, N n2);V operation2(N n1, N n2);
}

这个没有一个抽象方法,编译报错。

public interface CalcInterface<N, V> {    
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/330355.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hashCode和identityHashCode的区别你知道吗?

转载自 hashCode和identityHashCode的区别你知道吗&#xff1f;hashCode 关于hashCode参考之前的文章&#xff0c;点击参考之前文章。 identityHashCode identityHashCode是System里面提供的本地方法&#xff0c;java.lang.System#identityHashCode。 /*** Returns the same ha…

转:drop、truncate和delete的区别与选择

转自&#xff1a; https://blog.csdn.net/shadow_zed/article/details/78252494 &#xff08;1&#xff09;日志与事务 1.delete语句执行删除的过程是每次从表中删除一行&#xff0c;并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。 2.truncate tab…

Java高级篇——深入浅出Java类加载机制

转载自 Java高级篇——深入浅出Java类加载机制类加载器简单讲&#xff0c;类加载器ClassLoader的功能就是负责将class文件加载到jvm内存。类加载器分类从虚拟机层面讲分为两大类型的类加载器&#xff0c;一是Bootstrap Classloader即启动类加载器&#xff08;C实现&#xff09;…

康复题21

For a positive integer n lets define a function f: f(n)   - 1  2 - 3  ..  ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the positive integer n (1 ≤ n ≤ 1015). Output Print f(n) in a …

转: databasemetadata 无法获取数据库表备注的解决方法

转自&#xff1a; https://blog.csdn.net/10km/article/details/77389038 mysql/jdbc:设置useInformationSchematrue读取表注释信息(table_comment) 问题描述 今天在读取表的注释信息(COMMENT)时,发现返回的REMARKS字段返回居然是null. 以下是代码示例: DatabaseMetaData me…

IntegerCache的妙用和陷阱

转载自 IntegerCache的妙用和陷阱 考虑下面的小程序&#xff0c;你认为会输出为什么结果&#xff1f; public class Test {public static void main(String[] args) {Integer n1 123;Integer n2 123;Integer n3 128;Integer n4 128;System.out.println(n1 n2);System.ou…

转:线性代数知识汇总

https://blog.csdn.net/MyArrow/article/details/53365048 1. 线性代数知识图谱 线性代数是代数学的一个分支&#xff0c;主要处理线性关系问题。线性关系意即数学对象之间的关系是以一次形式来表达的。例如&#xff0c;在解析几何里&#xff0c;平面上直线的方程是二元一次…

康复题25

都说天上不会掉馅饼&#xff0c;但有一天gameboy正走在回家的小径上&#xff0c;忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了&#xff0c;这馅饼别处都不掉&#xff0c;就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了&#xff0c;所以gameboy马…

2020-09-17

https://www.zhihu.com/question/32729130

高级JAVA码农必须搞清楚它们的区别:instanceof、isInstance、isAssignableFrom

转载自 高级JAVA码农必须搞清楚它们的区别&#xff1a;instanceof、isInstance、isAssignableFrominstanceof、isInstance、isAssignableFrom这几个有没有接触过&#xff0c;或者接触过部分&#xff1f;定义a instanceof B a是B的实例&#xff0c;B是类或者接口、父类或父接口&…

康复题12

An elephant decided to visit his friend. It turned out that the elephants house is located at point 0 and his friends house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Deter…

Java架构师必看的10本书

转载自 Java架构师必看的10本书1、大型网站系统与JAVA中间件实践 本书围绕大型网站和支撑大型网站架构的Java中间件的实践展开介绍。 从分布式系统的知识切入&#xff0c;让读者对分布式系统有基本的了解&#xff1b;然后介绍大型网站随着数据量、访问量增长而发生的架构变迁&a…

转: Spark 的核心概念 RDD

转自&#xff1a; https://juejin.im/post/6844903826953076750 1.RDD 概述 1.1 什么是 RDD ? RDD(Resilient Distributed Dataset) 叫着 弹性分布式数据集 &#xff0c;是Spark 中最基本的抽象&#xff0c;它代表一个不可变、可分区、里面元素可以并行计算的集合。 RDD …

SpringMVC @Transactional的陷井大坑引发No Session found for current thread

一、TransactionManager事务配置 &#xff08;1&#xff09;注解配置 配置spring的xml加Transactional<tx:annotation-driven transaction-manager"transactionManager" proxy-target-class"true" /> &#xff08;2&#xff09;xml配置 <tx:adv…

POJ3904(BFS算法)

Problem Descrption Promble Description 定义一个二维数组&#xff1a; int maze[5][5] { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫&#xff0c;其中的1表示墙壁&#xff0c;0表示可以走的路&…

转:SparkConf 配置的用法

Spark应用程序的配置&#xff0c;用于将各种Spark参数设置为键值对。 大多数情况下&#xff0c;将使用新的SparkConf(),创建一个SparkConf对象&#xff0c;该对象将从您的应用程序中设置的任何spark.*的Java系统属性加载值。 在这种情况下&#xff0c;** 直接在SparkConf对象上…

Spring MVC 基于URL的拦截和映射规则

转载自 Spring MVC 基于URL的映射规则&#xff08;注解版&#xff09; url-pattern 如果看过前一篇入门的帖子&#xff0c;应该了解到spring mvc在启动前必须要在web.xml中配置servlet&#xff0c;这样才能拦截到想要映射的url地址。 <servlet><servlet-name>Spr…

转自: SparkConf 配置的概念和用法

一&#xff1a;概念 Spark应用程序的配置&#xff0c;用于将各种Spark参数设置为键值对。 大多数情况下&#xff0c;将使用新的SparkConf(),创建一个SparkConf对象&#xff0c;该对象将从您的应用程序中设置的任何spark.的Java系统属性加载值。 在这种情况下&#xff0c;* 直接…

HDU1231(最大连续子序列)

给定K个整数的序列{ N1, N2, ..., NK }&#xff0c;其任意连续子序列可表示为{ Ni, Ni1, ..., Nj }&#xff0c;其中 1 < i < j < K。最大连续子序列是所有连续子序列中元素和最大的一个&#xff0c; 例如给定序列{ -2, 11, -4, 13, -5, -2 }&#xff0c;其最大连续子…

漫画:什么是人工智能

转载自 漫画&#xff1a;什么是人工智能什么是人工智能&#xff1f;人工智能&#xff08;Artificial Intelligence&#xff09;&#xff0c;英文缩写为AI&#xff0c;它是研究、开发用于模拟和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。上个世纪50年代一次…