jdk8中几个核心的函数式接口笔记
1. Function接口
/**
* function 接口测试
* function 函数只能接受一个参数,要接受两个参数,得使用BiFunction接口
*/
public class FunctionTest {
@Test
public void Test (){
FunctionTest test = new FunctionTest();
// System.out.println(test.compute(4, x->x*2));
System.out.println(test.compute(2, v -> v * 3, v -> v * v));
System.out.println(test.compute2(2, v -> v * 3, v -> v * v));
System.out.println(test.compute3(2,3, (a, b)-> a+b));
System.out.println(test.compute4(2, 3, (a, b) -> a + b, a -> a * a));
}
/**
* function的第一个参数是输入, 第二个参数是输出
* @param a
* @param function
* @return
*/
public int compute(int a, Function<Integer, Integer> function) {
int result = function.apply(a);
return result;
}
/**
* compose 组合, 先执行传入的参数方法,再执行本身的方法
* @param a 输入
* @param func1 本身方法
* @param func2 传入的参数方法
* @return
*/
public int compute(int a, Function<Integer, Integer> func1, Function<Integer, Integer> func2) {
return func1.compose(func2).apply(a);
}
/**
* andThen 方法,先执行自己的方法,再执行传入的参数方法
* @param a
* @param func1 本身方法
* @param func2 传入的参数方法
* @return
*/
public int compute2(int a, Function<Integer, Integer> func1, Function<Integer, Integer> func2) {
return func1.andThen(func2).apply(a);
}
/**
* biFunction接口可传入两个参数
* @param a
* @param b
* @param biFunction
* @return
*/
public int compute3(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(a, b);
}
/**
* biFunction只有一个andThen方法(参数还是Function),因为他只返回一个值,不能返回两个值
* @param a
* @param b
* @param biFunction
* @param function
* @return
*/
public int compute4(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) {
return biFunction.andThen(function).apply(a,b);
}
}
// 模仿Function接口, 接受一个参数, 返回一个值
@FunctionalInterface
interface FakeInterface<J, R> {
R fuck(J t);
}
public class MyTest {
public String myTest(Integer a, FakeInterface<Integer, String> fakeInterface) {
System.out.println("执行我的fakeInterface中的方法, 传入一个参数");
String s = fakeInterface.fuck(a);
return s;
}
public static void main(String[] args) {
MyTest myTest = new MyTest();
String s = myTest.myTest(5, x -> String.valueOf(x));
System.out.println(s);
}
}
2. BiFunction接口
public class PersonTest {
@Test
public void test1() {
Person p1 = new Person("zhangsan", 20);
Person p2 = new Person("lisi", 30);
Person p3 = new Person("wangwu", 40);
List<Person> persons = Arrays.asList(p1, p2, p3);
PersonTest test = new PersonTest();
List<Person> result = test.getPersonByAge(30, persons);
result.forEach(x-> System.out.println(x.getUsername()));
}
@Test
public void test2() {
Person p1 = new Person("zhangsan", 20);
Person p2 = new Person("lisi", 30);
Person p3 = new Person("wangwu", 40);
List<Person> persons = Arrays.asList(p1, p2, p3);
PersonTest test = new PersonTest();
List<Person> result = test.getPersonByAge2(30, persons, (age, personList) -> {
return personList.stream().filter(p->p.getAge() > age).collect(Collectors.toList());
});
result.forEach(x-> System.out.println(x.getUsername()));
}
/**
* 强行使用lamboda来写代码
* @param age
* @param persons
* @return
*/
public List<Person> getPersonByAge(int age, List<Person> persons) {
// 定义函数操作
BiFunction<Integer, List<Person>, List<Person>> biFunction =
(ageOfPerson, personList) ->
personList.stream().filter(person -> person.getAge()> ageOfPerson).collect(Collectors.toList());
// 执行函数操作
return biFunction.apply(age, persons);
}
/**
* 不预先定义函数操作
* @param age
* @param persons
* @param biFunction 函数操作由用户传入,更加灵活
* @return
*/
public List<Person> getPersonByAge2(int age, List<Person> persons, BiFunction<Integer, List<Person>, List<Person>> biFunction) {
// 执行函数操作
return biFunction.apply(age, persons);
}
3. Predicate接口
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
* predicate接口, 是一个根据给定参数来决定返回bool值的判断式函数式接口
*/
public class PredicateTest {
@Test
public void Test1() {
Predicate<String> predicate = p->p.length() > 5;
System.out.println(predicate.test("hello"));
}
// 接口的简单练习
@Test
public void Test2() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
PredicateTest predicateTest = new PredicateTest();
System.out.println("打印出偶数: ");
predicateTest.conditionFilter(list, item->(item & 1) == 0);
System.out.println("---------------------------");
System.out.println("打印出奇数: ");
predicateTest.conditionFilter(list, item->(item & 1) == 1);
System.out.println("---------------------------");
predicateTest.conditionFilter(list, x->true);
System.out.println("----------------------------");
System.out.println("打印:, 大于5,并且偶数: ");
predicateTest.conditionAndFilter(list, x -> x > 5, x -> (x & 1) == 0);
System.out.println("----------------------------");
}
/**
* 函数式编程的一大特点就是将行为提到方法外面,由用户传入
* 调用时动态传入动作,更高层次的抽象化
* @param list
* @param predicate
*/
public void conditionFilter(List<Integer> list, Predicate<Integer> predicate) {
// 将通过条件判断的数字打印出来
for (Integer i : list) {
if (predicate.test(i)) {
System.out.println(i);
}
}
}
/**
* predicate接口的 与 运算, 打印出符合两个条件的结果
同理, 可理解predict中的其他逻辑运算
* @param list
* @param p1
* @param p2
*/
public void conditionAndFilter(List<Integer> list, Predicate<Integer> p1, Predicate<Integer> p2) {
for (Integer i : list) {
if (p1.and(p2).test(i)) {
System.out.println(i);
}
}
}
/**
* Predicate.isEqual() 方法用于判断两个参数是否相同, 依据就是 Objects#equals(Object, Object)}.
* 这个方法不怎么好理解, 说白了,就是调用传入参数的equals方法,
* 得到其方法的引用,而这个引用又刚好符合Predict函数的格式,可以作为Predict来使用
* @param object
* @return
*/
public Predicate<String> isEqual(Object object) {
return Predicate.isEqual(object);
}
@Test
public void Test3() {
PredicateTest predicateTest = new PredicateTest();
System.out.println(predicateTest.isEqual("test").test("test")); // true
System.out.println(predicateTest.isEqual("test").test("test2")); // false
System.out.println(predicateTest.isEqual(null).test("test")); // false
System.out.println(predicateTest.isEqual(null).test(null)); //true
}
}
4. Supplier接口
@Data
public class Student {
private String name = "zhangsan";
private Integer age = 20;
}
/**
* Supplier接口, 不接受参数, 返回一个结果
* 常用于 工厂
*
*/
public class SupplierTest {
// 最简单例子
@Test
public void Test() {
Supplier<String> supplier = () -> "hello world";
System.out.println(supplier.get());
}
/**
* 利用supplier生产学生
*/
@Test
public void Test2() {
Supplier<Student> supplier = ()->new Student();
System.out.println(supplier.get().toString());
// 更进一步, 调用Student的构造方法, 叫做构造方法的引用
// 不接受参数, 返回Student对象,符合函数式接口的要求
// 编译器会去Student找不带参数, 返回Student的构造方法
Supplier<Student> supplier2 = Student::new;
System.out.println(supplier2.get().toString());
}
}
5. Consumer接口
接收一个参数, 不返回值, 但可能改变传入的参数,通过这个改变的副作用来实现业务,list.forEach中就是一个Consumer接口作为参数
list.forEach(x->System.out.println(x));
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
public class MyTest {
public void myTest2(Integer a, Consumer<Integer> consumer) {
consumer.accept(a);
}
public static void main(String[] args) {
MyTest myTest = new MyTest();
myTest.myTest2(5, x-> {
System.out.println("可以操作x,但没有返回值");
});
}
}
jdk8中几个核心的函数式接口笔记的更多相关文章
- 乐字节-Java8核心特性实战之函数式接口
什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口(Functional Interface)就是一个有且仅有一 ...
- Java8内置的四大核心函数式接口
package java_8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import j ...
- Java8新特性(一)——Lambda表达式与函数式接口
一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...
- Java 8 特性 —— 函数式接口
函数式接口 概述:接口中只有一个抽象方法. 函数式接口,即适用于函数式编程场景的接口.而 Java 中的函数式编程体现就是 Lambda,所以函数式接口就是可以适用于 Lambda 使用的接口.只有确 ...
- JAVA8之函数式接口
由于JDK8已经发布一段时间了,也开始逐渐稳定,未来使用JAVA语言开发的系统会逐渐升级到JDK8,因为为了以后工作需要,我们有必要了解JAVA8的一些新的特性.JAVA8相对JAVA7最重要的一个突 ...
- Java8 新特性 函数式接口
什么是函数式接口 函数式接口是Java8引用的一个新特性,是一种特殊的接口:SAM类型的接口(Single Abstract Method).但是它还是一个接口,只是有些特殊罢了. 函数式接口的 ...
- java8学习之Supplier与函数式接口总结
Supplier接口: 继续学习一个新的函数式接口--Supplier,它的中文意思为供应商.提供者,下面看一下它的javadoc: 而具体的方法也是相当的简单,就是不接受任何参数,返回一个结果: 对 ...
- JDK8中Stream使用解析
JDK8中Stream使用解析 现在谈及JDK8的新特新,已经说不上新了.本篇介绍的就是Stream和Lambda,说的Stream可不是JDK中的IO流,这里的Stream指的是处理集合的抽象概念『 ...
- Java8 函数式接口 @FunctionalInterface以及常用Consumer<T>、Supplier<T>、Function<T, R>、Predicate<T>总结
首先看看什么是Lambda 表达式 Lambda是一个匿名函数,我们可以把Lambda表达式理解为一段可以传递的代码(将代码像数据一样传递):最简单的Lambda表达式可由逗号分隔的参数列表.-> ...
随机推荐
- 关于导入web项目之后项目名上有红叉,但是能够正常运行,代码不会报错的问题
解决方式之一: 1.进入项目包下的.settings目录 2.找到org.eclipse.wst.common.project.facet.core.xml文件,用记事本打开 3.将<runti ...
- go语言系列--golang在windows上的安装和开发环境goland的配置
在windows上安装golang软件 golang中国网址为:https://studygolang.com/dl 我的学习选择版本:1.12.5 golang 1.12.5版本更新的内容:gola ...
- (45)FreeRTOS学习之二
一:架构概述 FreeRTOS是一个相对较小的应用程序.最小化的FreeRTOS内核仅包括3个(.c)文件和少数头文件,总共不到9000行代码,还包括了注释和空行.一个典型的编译后(二进制)代码映像小 ...
- springboot(三).springboot用最简单的方式整合mybatis
Springboot整合mybatis 在众多的orm框架中,我使用最多的,最习惯的,也是目前使用最广泛的就是mybatis,接下来我们就去将springboot整合mybatis 对于spring ...
- Windows Server 部署WEB API时内部错误
Windows Server 部署WEB API时,发生HTTP 错误 500.21 - Internal Server Error,如图所示: 错误原因:IIS注册Framework4.0 解决方法 ...
- [CSP-S模拟测试]:数对(线段树优化DP)
题目传送门(内部题96) 输入格式 第一行一个整数$n$,接下来$n$行每行三个整数$a_i,b_i,w_i$. 输出格式 一行一个整数表示最大权值和. 样例 样例输入: 54 4 12 3 31 5 ...
- 不使用spring-boot-starter-parent进行依赖的版本管理
spring-boot-starter-parent 提供了Dependency Management 进行项目依赖的版本管理,默认的资源过滤和插件配置. 但是,当需要将其他项目作为parent 的时 ...
- springMVC @response 中文乱码解决
以下任选一种 在web.xml中: 添加一个过滤器(filter),注册 org.springframework.web.filter.CharacterEncodingFilter <filt ...
- 你是一直认为 count(1) 比 count(*) 效率高么?
MySQL count(1) 真的比 count(*) 快么? 反正同事们都是这么说的,我也姑且觉得对吧,那么没有自己研究一下究竟?如果我告诉你他们一样,你信么? 有 Where 条件的 count, ...
- TiDB单机安装测试
TiDB单机安装测试 CentOS7.3安装MySQL5.6 1:下载TiDB安装包# cd /opt/source# wget http://download.pingcap.org/tidb-la ...