当前位置: 首页 > news >正文

Java—JDK8新特性—函数式接口【内含思维导图】

目录

3.函数式接口

思维导图

3.1 什么是函数式接口

3.2 @functionalinterface注解 源码分析

3.3 Lambda表达式和函数式接口关系

3.4 使用函数式接口

3.5 内置函数式接口

四大核的函数式接口区别

3.5.1 Supplier 函数式接口源码分析

3.5.2 Supplier 函数式接口使用

3.5.3 Consumer 函数式接口源码分析

3.5.4 Consumer 函数式接口使用 

3.5.5 Function 函数式接口源码分析

3.5.6 Function 函数式接口使用 

3.5.7 Predicate 函数式接口源码分析

3.5.8 Predicate 函数式接口使用 


3.函数式接口

思维导图

3.1 什么是函数式接口

        如果一个接口中只包含一个抽象方法,这个接口称为函数式接口

如果一个接口包含,一个抽象方法、一个默认方法、一个静态方法,这个接口也是函数式接口


3.2 @functionalinterface注解 源码分析

        一个标记性注解

        在定义接口是函数式接口时,可以使用 @functionalinterface 注解进行标注,该注解会在编译器对这个接口进行检查

        如果使用@functionalinterface 注解来定义接口,编译器将会强制检查该接口,是否只有一个抽象方法,否则会报错。


3.3 Lambda表达式和函数式接口关系

        Lambda表达式就是一个函数式接口的实例

        如果一个对象是函数式接口的实例,那么该对象就可以用Lambda表达式,用匿名实现类表示的地方都可以用Lambda表达式替换


3.4 使用函数式接口

        可以使用JDK中内置的函数式接口,也可以自定义函数式接口

案例1:

先定义一个函数式接口

@FunctionalInterface
public interface MyFun<T> {T getValue(T t);
}

写一个实现类,FunctionInterfaceDemo1,进行对给定字符串转为大写

/*** 函数式接口使用*/
public class FunctionInterfaceDemo1 {public static void main(String[] args) {//需求: 使用函数式接口,将给定的字符转为大写//1.使用原始方法System.out.println(toUpperCase("hello"));//2.使用函数式接口String result1 = toLowerCase(new MyFun<String>() {@Overridepublic String getValue(String s) {return s.toUpperCase();}}, "hello");System.out.println("result1:" + result1);String result2 = toLowerCase( s -> s.toUpperCase(), "hello");System.out.println("result2:" + result2);}// 定义一个方法,将给定的字符串转为大写public static String toUpperCase(String str) {return str.toUpperCase();}/*** 将给定的字符串转为大写* @param function 函数式接口* @param str 给定字符串* @return*/public static String toLowerCase(MyFun<String> function, String str) {return function.getValue(str);}
}

运行结果:


3.5 内置函数式接口

在JDK8中内置了以下四大核的函数式接口

        Supplier接口、Consumer接口、Function接口、Predicate接口 

四大核的函数式接口区别

函数式接口参数类型返回类型使用说明
Consumer<T>Tvoid

对类型为T的对象应用操作,

包含方法:void accept(T t)

Supplier<T>T

返回类型为T的对象,

包含方法:T get()

Function<T,R>R

对类型为T的对象应用操作,

并返回结果为R类型的对象。包含方法:Rapply(T t)

Predicate<T>Tboolean

确定类型为T的对象是否满足某约束,

并返回布尔值。包含方法: booleantest(T t)

3.5.1 Supplier 函数式接口源码分析

3.5.2 Supplier 函数式接口使用

案例:

/*** JDK内置Supplier函数式接口的使用*/
public class SupplierDemo1 {public static void main(String[] args) {getMax(new Supplier<Integer>() {@Overridepublic Integer get() {//1.定义一个数组int[] arr = {12,23,34,94,555,60,27,38,99};//2.对数组进行排序Arrays.sort(arr);//3.将最大值返回return arr[arr.length - 1];}});getMax(() ->{//1.定义一个数组int[] arr = {12,23,34,94,555,60,27,38,99};//2.对数组进行排序Arrays.sort(arr);//3.将最大值返回return arr[arr.length - 1];});}private static void getMax(Supplier<Integer> supplier) {int max = supplier.get();System.out.println(max);}
}

运行结果:

3.5.3 Consumer 函数式接口源码分析

         消费一个数据,其数据类型由泛型参数决定

        Consumer 函数式接口,可以通过accept()方法参数传递过来的数据进行处理

        有参无返回接口

3.5.4 Consumer 函数式接口使用 

案例:

/*** JDK内置 消费型函数式接口 Consumer 使用*/
public class ConsumerDemo {public static void main(String[] args) {transform(new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println(s.toUpperCase());}});transform(s -> System.out.println(s.toUpperCase()));}public static void transform(Consumer<String> consumer) {consumer.accept("hello Consumer");}
}

3.5.5 Function 函数式接口源码分析

        根据一个类型的数据得到另外一个类型的数据,有参有返回

        Function转换型接口,对apply()方法传入的T类型数据进行处理,返回R类型的结果

3.5.6 Function 函数式接口使用 

案例:

/*** JDk内置功能型接口 Function 使用*/
public class FunctionDemo {public static void main(String[] args) {System.out.println(calculate(new Function<String, Integer>() {@Overridepublic Integer apply(String s) {return  Integer.parseInt(s);}}));System.out.println(calculate( s-> Integer.parseInt(s)));System.out.println(calculate1(s-> Integer.parseInt(s), i -> i +5));}public static int calculate(Function<String,Integer> function) {Integer value = function.apply("10");return value + 5;}public static int calculate1(Function<String,Integer> f1,Function<Integer,Integer> f2) {Integer value2 = f1.andThen(f2).apply("10");return value2;}
}

3.5.7 Predicate 函数式接口源码分析

        需要对某种类型的数据进行判断,从而得到一个Boolean值结果,常用于条件判断场景

3.5.8 Predicate 函数式接口使用  

/*** JDK内置断言函数式接口 Predicate 使用*/
public class PredicateDemo {public static void main(String[] args) {testName(s -> s.length() > 3,"张三");testAnd(s -> s.contains("A"),s -> s.contains("B"),"ABC");testAnd(s -> s.contains("A"),s -> s.contains("B"),"Abc");testOr(s -> s.contains("Z"),s -> s.contains("Y"),"Zxc");testOr(s -> s.contains("Z"),s -> s.contains("Y"),"zxc");testNegate(s -> s.contains("Q"),"iQ");testNegate(s -> s.contains("Q"),"asd");}/*** 使用Lambda表达式判断 名字长度是否超过3个字*/private static void testName(Predicate<String> predicate,String name) {System.out.println(predicate.test(name) ? "超过3个字" : "没有超过3个字");}/*** 使用Lambda表达式判断一个字符中,包含A,包含B* 使用了Predicate 中的 and 与* default Predicate<T> and(Predicate<? super T> other) {*         Objects.requireNonNull(other);*         return (t) -> test(t) && other.test(t);*     }*/private static void testAnd(Predicate<String> p1,Predicate<String> p2,String str) {System.out.println(p1.and(p2).test(str) ? "yes" : "no");}/*** 使用Lambda表达式判断一个字符中,可能包含Z 或者 可能 包含Y* 使用了Predicate 中的 or 或* default Predicate<T> or(Predicate<? super T> other) {*         Objects.requireNonNull(other);*         return (t) -> test(t) || other.test(t);*     }*/private static void testOr(Predicate<String> p1,Predicate<String> p2,String str) {System.out.println(p1.or(p2).test(str) ? "可能包含Z 或者 可能 包含Y" : "不包含Z 不包含Y");}/*** 使用Lambda表达式判断一个字符中,可能不包含Q* 使用了Predicate 中的* default Predicate<T> negate() {*         return (t) -> !test(t);*     } **/private static void testNegate(Predicate<String> p1, String str) {System.out.println(p1.negate().test(str) ? "不包含Q" : "包含Q");}
}

http://www.lryc.cn/news/66172.html

相关文章:

  • 【MySQL】外键约束和外键策略
  • 3. SQL底层执行原理详解
  • Bus动态刷新
  • 逆波兰式的写法
  • Linux系统日志介绍
  • 第三十二章 React路由组件的简单使用
  • “裸奔”时代下,我们该如何保护网络隐私?
  • c#笔记-方法
  • 054、牛客网算法面试必刷TOP101--堆/栈/队列(230509)
  • 怎么让chatGTP写论文-chatGTP写论文工具
  • springboot 断点上传、续传、秒传实现
  • 2023河南省赛vp题解
  • 港科夜闻|香港科大与香港资管通有限公司签署校企合作备忘录,成立校企合作基金促科研成果落地...
  • Neo4j 笔记
  • 数据库基础应用——概念模型
  • 【学姐面试宝典】前端基础篇Ⅴ——JS深浅拷贝、箭头函数、事件监听等
  • 最新研究,GPT-4暴露了缺点!无法完全理解语言歧义!
  • 商业数据挖掘-第一章-数据探索式分析-1
  • MybatisPlus是否防止SQL注入?
  • 5月第1周榜单丨飞瓜数据B站UP主排行榜(哔哩哔哩平台)发布!
  • 数据的插入删除和更新
  • C# byte[] 与 int 类型互转
  • MySQL---多表联合查询(上)(多表关系、外键约束、学生成绩多表关系、交叉连接查询)
  • 【iOS】—— RunLoop线程常驻和线程保活
  • Springcloud--docker快速入门
  • 基于AT89C51单片机的电子计数器设计与仿真
  • IT程序员如何面对35岁大龄问题?我从公司老板的角度聊聊
  • 【计算机专业漫谈】【计算机系统基础学习笔记】W2-2-2 模运算系统和补码表示
  • vue概述
  • SpringCloud-OpenFeign案例实战