Java内置的四大核心函数式接口

Consumer:消费型接口

对类型为T的对象应用操作,包含方法:void accept(T t)

public class TestLambda02 {
public static void main(String[] args) {
testConsumer(9999, (x) -> {
System.out.println("打游戏花费:" + x);
});
} public static void testConsumer(double money, Consumer<Double> consumer) {
consumer.accept(money);
}
}

Supplier:供给型接口

返回包含类型为T的对象,包含方法:T get()

public class TestLambda02 {
public static void main(String[] args) {
List<Integer> list = testSupplier(10, () -> (int)(Math.random() * 100));
list.forEach(System.out::println);
} public static List<Integer> testSupplier(int num, Supplier<Integer> supplier) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i ++) {
Integer n = supplier.get();
list.add(n);
}
return list;
}
}

Function<T, R>:函数型接口

对类型为T的对象应用操作,并返回结果,结果类型为R。包含方法:R apply(T t)

public class TestLambda02 {
public static void main(String[] args) {
String string = testFunction("哈哈哈哈哈哈哈", (str) -> str.substring(0, str.length() - 2));
System.out.println("---->" + string);
} public static String testFunction(String str, Function<String, String> function) {
return function.apply(str);
}
}

Predicate(T):断定型接口

确定类型为T的对象是否满足某约束,并返回boolean值。包含方法:boolean test(T t)

public class TestLambda02 {
public static void main(String[] args) {
System.out.println(testPredicate("哈哈哈哈皇后", (str) -> str.length() == 3));
} public static boolean testPredicate(String string, Predicate<String> predicate) {
return predicate.test(string);
}
}

PS:除了这四个接口,java还内置了其他很多接口,这里不再赘述。可以在rt.jar文件下的java/util/function包下查看。

Java8新特性 - Java内置的四大核心函数式接口的更多相关文章

  1. JDK8新特性03 Lambda表达式03_Java8 内置的四大核心函数式接口

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...

  2. Java8内置的四大核心函数式接口

    package java_8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import j ...

  3. java 8内置的四大核心函数式接口

    Consumer<T> : 消费性接口 返回值  void accept(T t); public void happy(double money, Consumer<Double& ...

  4. Java8新特性(一)——Lambda表达式与函数式接口

    一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...

  5. Java8新特性—四大内置函数式接口

    Java8新特性--四大内置函数式接口 预备知识 背景 Lambda 的设计者们为了让现有的功能与 Lambda 表达式良好兼容,考虑了很多方法,于是产生了函数接口这个概念. 什么是函数式接口? 函数 ...

  6. java8新特性—四大内置核心接口

    java8新特性-四大内置核心接口 四大内置核心接口 //消费型接口 Consumer<T>:: vode accept(T t); //供给型接口 Supplier<T>:: ...

  7. java8新特性——四大内置核心函数式接口

    在前面几篇简单介绍了一些Lambda表达式得好处与语法,我们知道使用Lambda表达式是需要使用函数式接口得,那么,岂不是在我们开发过程中需要定义许多函数式接口,其实不然,java8其实已经为我们定义 ...

  8. Java8新特性 -- 四大内置的核心函数式接口

    可以把这些函数式接口作为方法的参数. 1.0 核心内置函数式接口一: 消费型接口@FunctionalInterfacepublic interface Consumer<T> { voi ...

  9. java内置的四大函数式接口

    一.Consumer:消费型接口(void accept(T t)) 举一个例子 public class LambdaTest { @Test public void test() { happyT ...

随机推荐

  1. 在线http模拟工具

    在线http模拟工具http://www.atool.org/httptest.php

  2. shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句

    shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句条件语句 if(条件表达式) 动作1 else if(条件表达式) 动作2 else 动 ...

  3. Python3基础 str __add__ 拼接,原字符串不变

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  4. 重装Mac系统

    首先介绍重装macos的一些重要事项: 重装系统之前需要弄清楚本机上安装的系统版本. 重装系统是需要管理员账户的. 获取系统镜像有多种方式,其中最常见的方式是从appstore中获取,但要注意的是要确 ...

  5. 我的一个PLSQL【我】 循环嵌套、游标使用、变量定义、查询插入表、批量提交事务、字符串截取、动态sql拼接执行

    代码块: --CREATE OR REPLACE PROCEDURE PRO_REVENUE_STATISTICS --IS DECLARE --计数器 ins_counter PLS_INTEGER ...

  6. Spring cloud微服务安全实战-6-2JWT认证之认证服务改造

    首先来解决认证的问题. 1.效率低,每次认证都要去认证服务器调一次服务. 2.传递用户身份,在请求头里面, 3.服务之间传递请求头比较麻烦. jwt令牌. spring提供了工具,帮你在微服务之间传递 ...

  7. docker镜像的导入导出

    docker save > nginx.tar nginx:latest docker load < nginx.tar

  8. linux记录-安装zabbix监控系统

    1. 安装依赖yum -y install libcurl-devel libxml2-devel net-snmp net-snmp-devel2. 安装 nginxyum -y install n ...

  9. python 求交集、并集、差集

    需要用到set类型 交集,两种方法 retA = [i for i in listA if i in listB] retB = setA.intersection(setB) 并集 retC = s ...

  10. Win10下载安装PostgreSQL 11.1

    下载地址:https://get.enterprisedb.com/postgresql/postgresql-11.1-1-windows-x64.exe Installation Director ...