你眼中java8的样子
你眼中java8新特性的样子
文章目录
- 你眼中java8新特性的样子
- 1. 函数式接口
- 1. 定义
- 2. Lambda
- 1. 作用
- 2. 概要
- 3. 作用
- 4. 基本语法
- 3. function
- 4. supplier
- 1. coding
- 2. 简介
- 5. BinaryOperatorTest
- 1. coding
- 6. Optional
- 1. coding
- 7. 方法引用
- 1. 语义
- 2. 分类
1. 函数式接口
1. 定义
如果一个接口有且只有一个抽象方法,那么该接口接口一个函数式接口如果在接口上声明FunctionalInterfance,那么就会将按照函数式接口来要求该接口如果一个接口有且只有一个抽象方法,但是并没有声明FunctionalInterfance 注解,那么编译器依旧会将该接口看作sshi函数式接口
2. Lambda
1. 作用
1. Lamdba 表达式为java添加了缺失的函数式编程特性,是我们能将函数当作一等公民看待
2. 在将函数作为作为一等公民的语言中,Lamdba表达式的类型是函数。但在Java中,Lambda是对象,他们必须依附于一类特别的对象类型-函数式接口(functional interfance)
2. 概要
Java Lamdba表达式是一种匿名函数;它是没有声明的方法,即没有访问修饰符、返回值和名字
3. 作用
传递行为,不仅仅是值
提升抽象层次
API重用性更好
更加灵活
4. 基本语法
(arguments)-> {body}
demo
(arg1,arg2) -> {body}
(type1 arg1, type2 arg2) -> {body}
实例
- (int a,int b) -> {return a+b;}
- () -> {System.out.pringln(“1111”);}
- (String s) -> {System.out.pringln(s);}
- () -> 111
- ()
说明
1.一个参数可以省略小括号,一个公式可以省略花括号
2.类型可以推导,可以省略类型声明
3. function
package com.maidou.learning.java8.function;import java.util.function.BiFunction;
import java.util.function.Function;public class FunctionTest1 {public static void main(String[] args) {FunctionTest1 functionTest1 = new FunctionTest1();System.out.println(functionTest1.computer(2, value -> value + 2, value -> value * value));System.out.println(functionTest1.andThen(2, value -> value + 2, value -> value * value));System.out.println(functionTest1.computer(2,3 , (value1, value2) -> value1 + value2));System.out.println(functionTest1.andThen(2,3 , (value1, value2) -> value1 + value2, value -> value * value));}/*** @Author maicheng* @Description 先应用后作用于当前funcation* @Date 15:26 2022/10/16* @Param [a, funcation1, function2]* @return int**/public int computer(int a, Function<Integer, Integer> funcation1, Function<Integer, Integer> function2) {return funcation1.compose(function2).apply(a);}/*** @Author maicheng* @Description 先作用于当前funcation后应用* @Date 15:27 2022/10/16* @Param [a, funcation1, function2]* @return int**/public int andThen(int a, Function<Integer, Integer> funcation1, Function<Integer, Integer> function2) {return funcation1.andThen(function2).apply(a);}public int computer(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) {return biFunction.apply(a, b);}public int andThen(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function2) {return biFunction.andThen(function2).apply(a,b);}
}
4. supplier
1. coding
package com.maidou.learning.java8;/*** @Author maichen* @Description : 学生对象信息* @Date 23:12 2022/10/16**/
public class Student {private String name = "zhangsan";private int age = 20;public Student() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}package com.maidou.learning.java8;import java.util.function.Supplier;public class StudentTest {public static void main(String[] args) {// 方案一Student student = new Student();System.out.println(student.getName());// 方案二Supplier<Student> supplier = () -> new Student();System.out.println(supplier.get().getName());// 方案三Supplier<Student> supplier1 = Student::new;System.out.println(supplier1.get().getName());}
}
2. 简介
Supplier是Java8配合Lambda表达式和函数式接口编程组合使用的一个接口,对外表现为 ::
接口Supplier 最适合表示工厂。带有Supplier 的方法,通常应该限制输入工厂的类型参数使用有限制的通配符类型,以便客户端可以传入工厂,来创建制定类型的任意子类。
5. BinaryOperatorTest
1. coding
package com.maidou.learning.java8;import java.util.Comparator;
import java.util.function.BinaryOperator;public class BinaryOperatorTest {public static void main(String[] args) {BinaryOperatorTest binaryOperatorTest = new BinaryOperatorTest();System.out.println(binaryOperatorTest.getBinaryOperator(1,2, (a,b) -> a + b));System.out.println(binaryOperatorTest.getBinaryOperator(1,2, (a,b) -> a - b));binaryOperatorTest.getMin(1,2, Comparator.comparingInt(a -> a));}public int getBinaryOperator(int a, int b, BinaryOperator<Integer> binaryOperator) {return binaryOperator.apply(a,b);}public int getMin(Integer a, Integer b, Comparator<Integer> comparator) {return BinaryOperator.minBy(comparator).apply(a,b);}
}
6. Optional
1. coding
package com.maidou.learning.java8;import java.util.Optional;public class OptionalTest {public static void main(String[] args) {Optional<String> optional = Optional.of("111");System.out.println(optional.get());//判空optional.ifPresent(str -> System.out.println(str));}
}
7. 方法引用
1. 语义
方法引用实际上是lambda表达式的一种语法糖
可以将方法引用看作是一个函数指针,function pointer
classname::staticmethod
2. 分类
- 类名::静态方法
- 引用名(对象)::实例方法
- 类名::实例方法
你眼中java8的样子相关推荐
- acl在内核里的位置_Linux 进程在内核眼中是什么样子的?
本篇算是进程管理的的揭幕篇,简单介绍一个进程在内核眼里的来龙去脉,为接下来的进程创建,进程调度,进程管理等篇章做好学习准备. 从程序到进程再到内核 啥是程序,啥是进程,一张图可以给我们解释: 程序转换 ...
- 大爆炸之前的宇宙是什么样子?|赠书
来源:科研圈 宇宙蛋难题 古代的创世神话往往表现出奇妙的独创性,但是追根究底,它们只有两个基本的选择:宇宙要么是在有限的时间以前被创造的,要么就是永恒存在的. 以下是神圣的印度教经文<奥义书&g ...
- Embedding在网易严选搜索推荐中的应用
导读:向量化在业界的运用越来越广,近期也有许多文章分享过相关的主题.严选于18年下半年开始探索向量化在搜索推荐场景中的运用,从最开始基于商品召回用户的任务到后续的搜索召回.搜索个性化排序.搜索底纹.搜 ...
- 线程中可以创建进程吗_Linux 进程线程是如何创建的?
上文讲了<Linux进程在内核眼中是什么样子的?>,可以理解内核关于进程线程的所有管理就通过一个结构体 -- task_struct.知道了内核眼中进程的描述,本文通过三个例子站在用户态看 ...
- “猜你喜欢”是怎么猜中你心思的?
淘宝是怎么知道你想要买什么的 (文/Joseph A. Konstan & John Riedl)如今 ...
- 读叔本华之《人生的智慧》
作者简介 权本华(Arthur Schopenhauer,1788-1860)是德国著名哲学家,唯意志主义和现代悲观主义创始人.1788年2月22日诞生在但泽(今波兰格旦斯克)一个异常显赫的富商家庭, ...
- (整理)吊炸天的CNNs,这是我见过最详尽的图解!(上)
之前在CSDN上看到这篇文章,觉得通俗易懂,写的非常好.不过近来再次查看,发现文章的照片莫名其妙的没有了,没有图就根本看不懂了.找到了之前关注的微信公众号:AI传送门 . 在里面找到了这篇文章,决定再 ...
- 成长感悟:谁定义了你的大学生活
给予大学生的100条建议 这是 斯维青年 创始人 罗志远 在2021年5月25日,在朋友圈发布的一篇推文,觉得很有意义,以此共勉. 全文一共分为9个部分,下面一一介绍. 文章目录 给予大学生的100条 ...
- 2018大数据培训学习路线图(详细完整版)
2018大数据培训学习路线全课程目录+学习线路详解(详细完整版) 第一阶段:大数据基础Java语言基础阶段 1.1:Java开发介绍 1.1.1 Java的发展历史 1.1.2 Java的应用领域 1 ...
最新文章
- Python基础学习!容器:列表,元组,字典与集合!(2)
- 准确率至上已是过去式,这些趋势在2020年的AI领域更受关注
- 程序员面试题精选100题(29)-调整数组顺序使奇数位于偶数前面[算法]
- 深度学习-Tensorflow2.2-卷积神经网络{3}-电影评论数据分类/猫狗数据集实例-15
- php ci如何保证数据安全,浅谈php(codeigniter)安全性注意事项
- Oleans集群之Consul再解释
- Java 最高均薪 19015 元! 8 月程序员工资出炉,你拖后腿了吗?
- 如果可以,我们一起留在竹山。
- faststart可以卸载吗_你的手机你做主!免 ROOT 卸载安卓手机预装APP
- reids实现分布式锁两种方式,单机,集群
- AT89C51的矩阵键盘、跑马灯和呼吸灯设计
- 三分钟明白 Activiti工作流 -- java运用
- 用spring MVC 生成Excel和PDF
- H5纯静态页面分享到微信朋友圈带图片显示
- html中右侧三角形代码,纯CSS绘制三角形(各种角度)
- c语言求不定式的最大值,C语言之四则运算表达式求值(链栈)—支持浮点型数据,负数, 整型数据运算...
- Javascript或HTML代码该怎么进行压缩美化?
- 你了解这么多万兆以太网规范吗?
- fastboot unlock手机步骤
- 北京朝阳一互联网公司被警方一锅端了!23人被带走…这种岗位千万别干!
热门文章
- mysql连接出现(1040, ‘ny connections‘)
- 爬取盗墓笔记存放csv中
- Deepin 系统日志查看
- win8电脑打不开网页的修复方法--win10专业版
- 2021-06-272021年施工员-土建方向-岗位技能(施工员)试题及解析及施工员-土建方向-岗位技能(施工员)作业模拟考试
- 数值计算——number-precision
- 江浙沪地区计算机考研高效排名,南京这五所双非大学,就业容易超末流985,江浙沪认可度较高...
- 云隙服务器怎么设置,云隙-CloudGap
- 如何使用css实现三角形
- c语言追逐游戏,Unity一款快节奏的汽车追逐游戏模板(自动支持多种分辨率和纵横比)...