什么是函数编程接口?

约束:抽象方法有且只有一个,即不能有多个抽象方法,在接口中覆写Object类中的public方法(如equals),不算是函数式接口的方法。

被@FunctionalInterface注解该接口,没有该注解的接口满足约束也行。

在Java8中,满足下面任意一个条件的接口都是函数式接口:

  • 被@FunctionalInterface注释的接口,满足函数式接口的约束。
  • 没有被@FunctionalInterface注释的接口,但是满足函数式接口的约束。
  • @函数式的约束:
    • 接口有且只能有个一个抽象方法,只有方法定义,没有方法体。
    • 在接口中覆写Object类中的public方法,不算是函数式接口的方法。
    • 在接口中的default方法,不算是函数式接口的方法。
    • 在接口中的static方法,不算是函数式接口的方法。

自定义一个函数式编程接口

/**
* 自定义一个函数式编程接口
* 函数式编程只有一个抽象方法,所以默认的是实现的是这个抽象方法
* @param <T>
* @param <R>
*/
@FunctionalInterface
public interface CalcFunctionInterface<T, R> {
/**
* 计算t1和t2
*
* @param t1
* @param t2
* @return
*/
R calc(T t1, T t2);
}

传入不同calc函数实现的对象,进行调用

相当于以前创建CalcFunctionInterface的匿名类,重写了calc方法(由于只有一个抽象方法,所以默认就是calc方法)

 /**
* 相当于一个类实现了CalcFunction接口中的唯一一个函数calc
* 然后在利用多态,调用calc函数,传入两个参数,进行计算
*/
@Test
public void add(){
CalcFunctionInterface<Integer, Integer> add = (t1, t2) -> t1+t2;
Integer calc = add.calc(2, 3);
System.out.println(calc);
//
}

传入一个匿名类对象,进行方法调用calc

@Test
public void multiply(){
// 相当于通过匿名类的形式传入一个实现了CalcFunctionInterface接口的子类对象,重写了该接口的方法
Integer calc = FunctionalInterfacesTest.calc(2, 3, (t1, t2) -> t1 * t2);
System.out.println(calc);
//
} /**
* 接受了一个对象,利用对象的calc方法计算
*/
public static Integer calc(Integer i1, Integer i2, CalcFunctionInterface<Integer, Integer> calc){
return calc.calc(i1,i2);
}

便捷的引用类的构造器及方法

一个Convert接口

@FunctionalInterface
public interface Convert<F, T> {
T convert(F from);
}

lambda表达式的形式重写该函数式编程的唯一接口

@Test
public void testLambda(){
Convert<String, Integer> stringIntegerConvert = (from -> Integer.valueOf(from));
Integer convert = stringIntegerConvert.convert("123");
System.out.println(convert);
//
}

下面使用"::"运算符更精简

静态方法

@Test
public void testStaticMethod(){
Convert<String, Instant> stringInstantConvert = Instant::parse;
Instant convert = stringInstantConvert.convert("2019-04-25T16:09:03.852Z");
System.out.println(convert);
// 2019-04-25T16:09:03.852Z
}

实例方法

/**
* 实例对象的方法
*/
@Test
public void testObjectMethod(){
Something something = new Something();
Convert<String, String> startsWith = something::startsWith;
String convert = startsWith.convert("123");
System.out.println(convert);
//
}
class Something {
public String startsWith(String s) {
return String.valueOf(s.charAt(0));
}
}

对象的构造方法

 /**
* 调用对象的构造方法
*/
@Test
public void testConstructor(){
PersonFactory<Person> personFactory = Person::new;
Person person = personFactory.create("Chris", "Paul");
System.out.println(person);
// Person(firstName=Chris, lastName=Paul)
}
/**
* Person 工厂
*/
@FunctionalInterface
public interface PersonFactory<P extends Person> {
P create(String firstName, String lastName);
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
class Person {
private String firstName;
private String lastName;
}

Java8的内置函数式编程接口

https://juejin.im/post/5c7d1254e51d45720f72264c

https://www.cnblogs.com/theRhyme/p/10774341.html

https://mp.weixin.qq.com/s?__biz=MzIzMzgxOTQ5NA==&mid=2247483845&idx=1&sn=08990fd78e4f62ddf38238660cc4dd64&chksm=e8fe9dccdf8914da580e13a9fc5fee64c135f55d11f3c2de5731f052fa9f1a39060ed6375110&scene=21#wechat_redirect

https://www.exception.site/java8/java8-new-features

Java8自定义函数式编程接口和便捷的引用类的构造器及方法的更多相关文章

  1. Java8内置的函数式编程接口应用场景和方式

    首先,我们先定义一个函数式编程接口 @FunctionalInterface public interface BooleanFunctionalInterface<T> { boolea ...

  2. 谈一谈Java8的函数式编程(二) --Java8中的流

    流与集合    众所周知,日常开发与操作中涉及到集合的操作相当频繁,而java中对于集合的操作又是相当麻烦.这里你可能就有疑问了,我感觉平常开发的时候操作集合时不麻烦呀?那下面我们从一个例子说起. 计 ...

  3. 快速掌握Java8 Stream函数式编程技巧

    函数式编程优势 "函数第一位",即函数可以出现在任何地方. 可以把函数作为参数传递给另一个函数,还可以将函数作为返回值. 让代码的逻辑更清晰更优雅. 减少了可变量(Immutabl ...

  4. 谈一谈Java8的函数式编程 (三) --几道关于流的练习题

    为什么要有练习题?    所谓学而不思则罔,思而不学则殆,在系列第一篇就表明我认为写博客,既是分享,也是自己的巩固,我深信"纸上得来终觉浅,绝知此事要躬行"的道理,因此之后的几篇博 ...

  5. java8 函数式编程接口

    java8 函数式接口java.util.function.* @param T 入参类型 @param R 出参类型 1. Function <T,R> 例: Function<I ...

  6. java8 lambda 函数式编程

    package com.atguigu.java8; import java.util.ArrayList; import java.util.Comparator; import java.util ...

  7. [一] java8 函数式编程入门 什么是函数式编程 函数接口概念 流和收集器基本概念

      本文是针对于java8引入函数式编程概念以及stream流相关的一些简单介绍 什么是函数式编程?   java程序员第一反应可能会理解成类的成员方法一类的东西 此处并不是这个含义,更接近是数学上的 ...

  8. Java8 函数式编程详解

    Java8 函数式编程详解 Author:Dorae Date:2017年11月1日23:03:26 转载请注明出处 说起Java8,可能很多人都已经知道其最大的改进,就是引入了Lambda表达式与S ...

  9. Java8函数式编程探秘

    引子 将行为作为数据传递 怎样在一行代码里同时计算一个列表的和.最大值.最小值.平均值.元素个数.奇偶分组.指数.排序呢? 答案是思维反转!将行为作为数据传递. 文艺青年的代码如下所示: public ...

随机推荐

  1. 常用git的命令

    常用git的命令 详解git fetch与git pull的区别 Git放弃本地所有修改,强制更新: git fetch --all git reset --hard origin/master 说明 ...

  2. fcn训练及预测tgs数据集

    一.背景 kaggle上有这样一个题目,关于盐份预测的语义分割题目.TGS Salt Identification Challenge | Kaggle  https://www.kaggle.com ...

  3. [ZZ] 如何在多版本anaconda python环境下转换spyder

    https://www.zhihu.com/people/alexwhu/answers 使用anaconda的话,可以参考以下步骤: 1.打开anaconda navigator,选择左侧的环境菜单 ...

  4. 一种C语言实现面向对象特性的继承,多态

    基类: //.h typedef int (*TELE_SEND_CB)(char *pdata, int len); //函数表结构 typedef struct tele_pro_base_vtb ...

  5. SQL脚本--总耗CPU最多的前个SQL --平均耗CPU最多的前个SQL

    --总耗CPU最多的前个SQL SELECT TOP 20 total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数], qs.t ...

  6. python基础知识11---函数1

    函数 一.背景 在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处 ...

  7. Kong(V1.0.2) Securing the Admin API

    Introduction Kong的Admin API为Services, Routes, Plugins, Consumers, and Credentials的管理和配置提供了一个RESTful接 ...

  8. 填坑:Java 中的日期转换

    我们之前讨论过时间,在Java 中有一些方法会出现横线?比如Date 过期方法. 参考文章:知识点:java一些方法会有横线?以Date 过期方法为例 Java中的日期和时间处理方法 Date类(官方 ...

  9. 关于破解visualsvn 我这里是版本是5.2.1

    1.首先备份当前安装visualSVN文件的bin目录,万一出错还能反个水.一般默认安装路径是C:\Program Files (x86)\VisualSVN\bin 2.然后运行ildasm,Win ...

  10. 嵌入式 printf函数

    来自:https://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html #include <stdio.h> #includ ...