Function 提供了一个抽象方法  R apply(T t) 接收一个参数 返回 一个值,还有两个默认方法和一个静态方法

compose 是一个嵌套方法,先执行before.apply() 得到运算后的值,再执行apply(),andthen则相反

identity 输入一个值则返回一个值,t -> t 实际是 apply() 这个方法的实现

另外还有BiFunction、toIntFunction、DoubleToIntFunction 等函数式接口,区别在于参数的不同,比如LongToIntFunction 是接收一个Long参数,返回一个Int值

R apply(T t);

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
} default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
} static <T> Function<T, T> identity() {
return t -> t;
}

方法示例:

arrToArrayList 是在方法体里运算,而 arrToList 则将方法作为参数传递进去

package jdk8.function;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function; public class FunctionTest {
public static void main(String[] args) {
Integer[] i = {, , , };
Function<Integer[], ArrayList<Integer>> arrayListFunction = FunctionTest::arrToArrayList;
arrayListFunction.apply(i).forEach(System.out::println); ArrayList<Integer> list = FunctionTest.arrToList(i, l -> new ArrayList<>(Arrays.asList(l)));
list.forEach(System.out::println); FunctionTest.arrToList(i, l -> new HashSet<>(Arrays.asList(l))).forEach(System.out::println);
FunctionTest.arrToList(i, l -> new LinkedList<>(Arrays.asList(l))).forEach(System.out::println); FunctionTest.arrToList(i, l -> new LinkedList<>(Arrays.asList(l))).forEach(System.out::println); System.out.println(FunctionTest.compute(, s -> s + "百")); System.out.println(FunctionTest.addition("hello", "wolrd", (a, b) -> a + " " + b)); Function<String,String> function = Function.identity();
function.apply("hello wolrd");
} //将数组转为ArrayList
private static <T> ArrayList<T> arrToArrayList(T[] t) {
Objects.requireNonNull(t);
return new ArrayList<>(Arrays.asList(t));
} private static<T,R> R arrToList(T[] t, Function<T[], R> function) {
Objects.requireNonNull(t);
return function.apply(t);
} public static String compute(int a, Function<Integer, String> function) {
return function.apply(a); //function.apply() 接收实际的行为
} public static String addition(String a, String b, BiFunction<String, String, String> biFunction) {
return biFunction.apply(a, b);
}
}

Java函数式接口Function的更多相关文章

  1. Java 函数式接口

    目录 Java 函数式接口 1. 函数式接口 1.1 概念 1.2 格式 1.3 函数式接口的使用 2. 函数式编程 2.1 Lambda的延迟执行 性能浪费的日志案例 使用Lambda表达式的优化 ...

  2. 常用的函数式接口Function接口和常用的函数式接口Function接口默认方法andThen

    常用的函数式接口Function接口 package com.yang.Test.FunctionStudy; import java.util.function.Function; /** * ja ...

  3. Java函数式接口Consumer

    Consumer是java8提供的函数式接口之一,意思为消费者,接受参数而不返回值 void accept(T t); default Consumer<T> andThen(Consum ...

  4. Java函数式接口

    函数式接口定义且只定义了一个抽象方法.函数式接口的抽象方法的签名称为函数描述符.Java 8的java.util.function包中引入了几个新的函数式接口. 1.Predicate java.ut ...

  5. java8 函数式接口——Function/Predict/Supplier/Consumer

    Function 我们知道Java8的最大特性就是函数式接口.所有标注了@FunctionalInterface注解的接口都是函数式接口,具体来说,所有标注了该注解的接口都将能用在lambda表达式上 ...

  6. Java 函数式接口 lambda

    import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Demo1 { ...

  7. Java函数式接口与Lambda表达式

    什么是函数式接口? 函数式接口是一种特殊的接口,接口中只有一个抽象方法. 函数式接口与Lambda表达式有什么关系? 当需要一个函数式接口的对象时,可以提供一个lambda表达式. package l ...

  8. JAVA 函数式接口与c#委托对应关系(一)

    C# Action委托 VS JAVA Action 接口函数 1.c#:Action 封装一个方法,该方法不具有参数并且不返回值. 构造实体类类 using System; namespace Ac ...

  9. java8 四大核心函数式接口Function、Consumer、Supplier、Predicate(转载)

    Function<T, R> T:入参类型,R:出参类型 调用方法:R apply(T t); 定义函数示例:Function<Integer, Integer> func = ...

随机推荐

  1. 掌握Spark机器学习库-06-基础统计部分

    说明 本章主要讲解基础统计部分,包括基本统计.假设检验.相关系数等 数据集 数据集有两个文件,分别是: beijing.txt 北京历年降水量,不带年份 beijing2.txt 北京历年降水量,带年 ...

  2. [Android]AndroidDesign中ActionBar探究2 嵌入Fragment

    上一节我们只是简单了介绍了Android Design风格中的ActionBar的简单实用,如添加MenuItem,这节我们会进一步了解ActionBar的其他功能. 在Android Develop ...

  3. 快速开发框架天梭(Tissot)

    天梭(Tissot)集成SpringBoot+Dubbo等主流互联网技术栈,高度集成.优化方便快速搭建应用.某互金科技公司内部孵化框架,已应用于公司90%业务系统. 框架划分模块有: tissot-c ...

  4. chat - 和数据集沟通的指令稿

    总览 SYNOPSIS chat [ options ] script 描述 DESCRIPTION chat 程式在電腦與數據機之間定義溝通交換事宜. 它最主要的目的是用來在點對點協定的隱形程式 ( ...

  5. html嵌入pdf && html嵌入多媒体文件,word,flash,pdf,音视频

    <object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="1000" hei ...

  6. IIS+php服务器无法上传图片解决办法

    查找网上资料,发现php.ini下面有2个地方关于上传的配置: file_uploads = On  这里设置是否允许HTTP上传,默认应该为ON的 ;upload_tmp_dir=  这里设置上传文 ...

  7. Spring Data Redis整体介绍 (一)

    为什么使用Spring Data Redis 首先Spring Data Redis 是Spring 框架提供的用于操作Redis的客户端. Spring框架是一个全栈Java程序框架,通过DI.AO ...

  8. react之webpack

    1. 下载相关模块包 * 创建package.json ``` npm init ``` * react相关库 package-lock.json ``` npm install react reac ...

  9. 原生javascript实现call、apply和bind的方法

    var context = {id: 12}; function fun (name, age) { console.log(this.id, name, age) } bind bind() 方法会 ...

  10. CSU 2018年12月月赛 A 2213: Physics Exam

    Description 高中物理老师总认为给学生文本形式的问题比给纯计算形式的问题要求更高.毕竟,学生首先得阅读和理解问题. 因此,他们描述一个问题不像”U=10V,I=5A,P=?”,而是”有一个含 ...