java8 API 函数式接口
简介
14年,Oracle公司如期发布了Java 8正式版,Java8提供了强大的流式处理及函数式接口编程
函数式接口编程,相信很多人在javascript中都使用过,比如回调函数,如今Java8也吸收了诸多的其它语言优点。
线程创建(8以前的写法)
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.printf(Thread.currentThread().getName());
}
});
thread.start();
Java8的函数式接口写法
//1Java8 lambda表达式写法
Thread thread = new Thread(() -> {
System.out.printf(Thread.currentThread().getName());
});
//对于{}里面只有一条语句的写法
Thread thread1 = new Thread(() -> System.out.println(Thread.currentThread().getName()));
//这种写法,不知道各位看官看着是什么感想
Student student = new Student("yang", 2);
new Thread(student::getName);
对比一下
List<Student> list = new ArrayList<>();
list.add(new Student("yang", 20));
list.add(new Student("hehe", 25));
list.add(new Student("jiana", 18));
//对集合进行排序,以前的写法
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge(); }
});
//Java8 lambda表达式的写法
Collections.sort(list, (o1, o2) -> o1.getAge()-o2.getAge());
Java8函数式编程可以使用lambda表达式,让代码更加简洁高效,当然对于许多如果刚接触的小伙伴来说,如果没有经过学习lambda式的写法,看起来也会云里雾里的,毕竟写法是高效了,但可读性确实要差一些了,对于改bug的同鞋来讲,难度是否又增加许多?
JDK 1.8 API包含了很多内建的函数式接口,在老Java中常用到的比如Comparator或者Runnable接口,这些接口都增加了@FunctionalInterface注解以便能用在lambda上。
标注为FunctionalInterface的接口被称为函数式接口,该接口只能有一个自定义方法,但是可以包括从object类继承而来的方法。如果一个接口只有一个方法,则编译器会认为这就是一个函数式接口。是否是一个函数式接口,需要注意的有以下几点:
1.该注解只能标记在”有且仅有一个抽象方法”的接口上。
2.JDK8接口中的静态方法和默认方法,都不算是抽象方法。
3.接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。
4.该注解不是必须的,如果一个接口符合”函数式接口”定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。
5.在一个接口中定义两个自定义的方法,就会产生Invalid ‘@FunctionalInterface’ annotation; FunctionalInterfaceTest is not a functional interface错误.
现如今,我们则从Function常用函数入口,真正了解一下。
1.Function:接受一个对象,返回一个对象
/**
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
Function 内部有一个有一个apply(待子类实现)的方法,调用apply,回传R结果,通过泛型保证apply函数的入参入及返回值的类型。
基本使用:
对数据进行处理将String类型转换成double类型,代码如下:
//先定义好这个函数功能
Function<String, Double> stringToDouble = ((string) -> Double.parseDouble(string));
//再使用函数功能
System.out.println(stringToDouble.apply("20.25"));
异步回调处理
//将student的名字,通过回调的方式放入name变量中,name变量必须是Atomica类型的,因为交给别人,别人可以用线程来做
public class MainTest {
public static void main(String[] args) {
Student student2 = new Student("yanchuanbin", 25);
AtomicReference<String> name = new AtomicReference<>("");
String otherParams = "";
new Operator(student2).handler(otherParams, (student) -> {
System.out.println("here1 = " + Thread.currentThread().getName());
name.set(student.getName());
return null;
});
}
}
class Operator {
Student student;
public Operator(Student student) {
this.student = student;
}
public void handler(String otherParams, Function<Student, Void> callback) {
System.out.println("here2 = " + Thread.currentThread().getName());
ThreadFactory threadFactory;
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> callback.apply(student));
}
}
输出
here2 = main
here1 = pool-1-thread-1
2.Supplier:生产者,无参数,只有返回值
Supplier生产者,提前定义好生产方法,通过get()获取返回值,源码如下
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
作为生产者,基本使用
// Supplier<Student> supplier2 = new Supplier() {
// @Override
// public Student get() {
// return new Student("aa",new Random().nextInt(20);
// }
// };
Supplier<Student> supplier = () -> new Student("yanchuanbin", 2);
Student student = supplier.get();
3.Consumer:消费者,有入参,但无返回值
Consumer消费者,传入一个参数,消费后无返回值,源码如下
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
基本使用
Student student = new Student("yanchuanbin", 25);
//旧写法
// Consumer<Student> consumer = new Consumer<Student>() {
// @Override
// public void accept(Student student) {
// System.out.println(student.getAge());
// }
// };
//lambad写法
Consumer<Student> consumer = (student1 -> System.out.println(student1.getAge()));
consumer.accept(student);
4.Predicate:断言,有参数T,返回固定类型boolean
断言式接口其参数是<T,boolean>,源码如下
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
基本使用
Predicate<Student> studentEqualsPredicate = (student) -> student.getAge() > 10;
Student student = new Student("yanchuanbin", 25);
System.out.println(studentEqualsPredicate.test(student));
输出
true
5.UnaryOperator:接收T对象,返回T对象
对对象进行处理,处理后,将原来的对象返回,入参和返回值都是T,源码如下:
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
/**
* Returns a unary operator that always returns its input argument.
* @param <T> the type of the input and output of the operator
* @return a unary operator that always returns its input argument
*/
static <T> UnaryOperator<T> identity() {
return t -> t;
}
}
基本使用
UnaryOperator<Student> identity = (x -> {
return new Student(x.getName(), x.getAge()+1);
});
//生产一个大一岁的哥出来,名字是一样的
Student student1 = identity.apply(student);
6.BinaryOperator:传入两个T对象,返回T对象
继承自BiFunction,传入两个T类型的对象,返回一个T类型的对象,源码如下
* @param <T> the type of the operands and result of the operator
*
* @see BiFunction
* @see UnaryOperator
* @since 1.8
*/
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
BiFunction源码如下
@FunctionalInterface
public interface BiFunction<T, U, R> {
/**
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(T t, U u);
基本使用
//传入两个学生,返回两个学生中年龄较大的学生对象
BinaryOperator<Student> getMaxAgeStudentHandler =
(student1, student2) -> student1.getAge() > student2.getAge() ? student1 : student2;
Student student1 = new Student("ya",20);
Student student2 = new Student("wa",25);
Student maxAgeStudent = getMaxAgeStudentHandler.apply(student1,student2);
java8 API 函数式接口的更多相关文章
- [译]Java8的函数式接口
Java8引入了 java.util.function 包,他包含了函数式接口,具体的描述在以下api说明文档中: 函数式接口为lambda表达式和方法引用提供目标类型.每个函数式接口有一个单独的抽象 ...
- java8的函数式接口
函数式接口 就是在java8里允许你为一个接口(只有一个实现的,声明为FunctionalInterface注解的)实现一个匿名的对象,大叔感觉它与.net平台的委托很类似,一个方法里允许你接收一个方 ...
- JAVA8之函数式接口
由于JDK8已经发布一段时间了,也开始逐渐稳定,未来使用JAVA语言开发的系统会逐渐升级到JDK8,因为为了以后工作需要,我们有必要了解JAVA8的一些新的特性.JAVA8相对JAVA7最重要的一个突 ...
- Java8 Functional(函数式接口)
Functional 函数式(Functional)接口 只包含一个抽象方法的接口,称为函数式接口. 你可以通过 Lambda 表达式来创建该接口的对象.(若 Lambda 表达式抛出一个受检异常(即 ...
- java8 常用函数式接口
public static void main(String[] args) { // TODO Auto-generated method stub //函数式接口 Function<Inte ...
- Java8常见函数式接口总结
函数式接口 函数式接口:有且仅有一个抽象方法的接口. 使用@FunctionalInterface注解来标记.如果接口不是函数式接口就会编译出错 满足条件的接口即使不加上注解,那也是函数式接口 函数式 ...
- JDK1.8新特性(一) ----Lambda表达式、Stream API、函数式接口、方法引用
jdk1.8新特性知识点: Lambda表达式 Stream API 函数式接口 方法引用和构造器调用 接口中的默认方法和静态方法 新时间日期API default Lambda表达式 L ...
- 乐字节-Java8核心特性实战之函数式接口
什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口(Functional Interface)就是一个有且仅有一 ...
- java8学习之Supplier与函数式接口总结
Supplier接口: 继续学习一个新的函数式接口--Supplier,它的中文意思为供应商.提供者,下面看一下它的javadoc: 而具体的方法也是相当的简单,就是不接受任何参数,返回一个结果: 对 ...
- java代码之美(14)---Java8 函数式接口
Java8 函数式接口 之前写了有关JDK8的Lambda表达式:java代码之美(1)---Java8 Lambda 函数式接口可以理解就是为Lambda服务的,它们组合在一起可以让你的代码看去更加 ...
随机推荐
- 重磅发布 阿里云数据中台全新产品DataTrust聚焦企业数据安全保障
简介: DataTrust(隐私增强计算产品)是基于阿里云底层多项基础安全能力,经过阿里云数据中台丰富的客户业务实践,构建的一款为企业数据安全流通的产品. 随着包括零售.制造.金融等多行业数字化转型加 ...
- [GF] 与 Laravel 设计相近的 Golang 框架 GoFrame
在 GoFrame (gogf/gf) 框架中有明确的代码分层设计,分别是 api, service, dao, model. model (结构模型)一般由工具自动生成,用于定义数据结构,只可被 m ...
- [FE] JS 判断当前是否在微信浏览器中的最新代码
注意以下使用了 const 定义未改变的变量,没有使用 var. function isWeChatBrowser () { const ua = window.navigator.userAgent ...
- WPF 已知问题 资源字典树引用与资源寻找的坑
大家都知道,在 WPF 里面,可以让资源字典合并其他资源字典,从而定义出资源字典引用树.然而在资源字典引用树里面,如果没有理清关系,将可以作出一个超级复杂的引用关系网.如果在性能优化中,将网断开部分, ...
- STM32定时器原理
一.简介 不同的芯片定时器的数量不同,STM32F10x中一共有11个定时器,其中2个高级控制定时器,4个普通定时器和2个基本定时器,以及2个看门狗定时器和1个系统嘀嗒定时器. 基本定时器:TIM6. ...
- aspnetcore插件开发dll热加载
该项目比较简单,只是单纯的把业务的dll模块和controller的dll做了一个动态的添加删除处理,目的就是插件开发.由于该项目过于简单,请勿吐槽.复杂的后续可以通过泛型的实体.dto等做业务和接口 ...
- Solution Set - 加训 CF!
加训一些 CF 题,这里写一些简要题解,可能是草稿. 暂定只做 Div.1 的题和 Div.1+Div.2 的后一半题.
- XTuner 微调 LLM实操-书生浦语大模型实战营第二期第4节作业
这一作业中提及的解释比较少,更多的只是一些步骤截图.这是因为教程中已经提及了几乎所有的细节信息,没有什么需要补充的.这个页面相较于官方教程的部分解释得过于详细的内容甚至是有所删减的.比如关于文件路径可 ...
- C++ 资源大全:标准库、Web框架、人工智能等 | 最全整理
C++ 资源列表,内容包括: 标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等 目录 进程间通信 Json 日志 机器学习 数学 内存分配 多媒体 网络 PDF 物理学 ...
- NODEJS通过发送json数据查询目标服务,实现服务器状态监控,发现异常发送到微信群提醒
root@aea87fa6e6a2:/home/node# cat login2.js const request = require('request-promise'); const moment ...