通过解读源码,可以发现Supplier 在Java 8中,是一个功能接口,内置的一个函数式接口,可以用来创建新的对象,有别于关键字 new;它不带任何参数并返回结果。

Supplier.java

@FunctionalInterface

public interface Supplier {

T get();

}

Supplier

当前这个示例使用Supplier返回当前日期时间

Java8Supplier1.javaout

package com.willchu;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import java.util.function.Supplier;

public class Java8Supplier1 {

private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public static void main(String[] args) {

Supplier s = () -> LocalDateTime.now();

LocalDateTime time = s.get();

System.out.println(time);

Supplier s1 = () -> dtf.format(LocalDateTime.now());

String time2 = s1.get();

System.out.println(time2);

}

}

控制台输出:

2020-03-02T16:10:49.281223

2020-03-02 16:10:49

2.返回一个Supplier

Java8Supplier2.java

package com.willchu;

import java.util.ArrayList;

import java.util.List;

import java.util.function.Supplier;

public class Java8Supplier2 {

public static void main(String[] args) {

Java8Example obj = new Java8Example();

List list = obj.supplier().get();

}

public Supplier> supplier() {

// lambda

// return () -> new ArrayList<>();

// constructor reference

return ArrayList::new;

}

}

3.工厂,Factory

返回Developer对象的简单工厂方法

Java8Supplier3.java

package com.willchu;

import java.math.BigDecimal;

import java.time.LocalDate;

import java.util.function.Supplier;

public class Java8Supplier3 {

public static void main(String[] args) {

Developer obj = factory(Developer::new);

System.out.println(obj);

Developer obj2 = factory(() -> new Developer("mkyong"));

System.out.println(obj2);

}

public static Developer factory(Supplier extends Developer> s) {

Developer developer = s.get();

if (developer.getName() == null || "".equals(developer.getName())) {

developer.setName("default");

}

developer.setSalary(BigDecimal.ONE);

developer.setStart(LocalDate.of(2017, 8, 8));

return developer;

}

}

Developer.java

package com.willchu;

import java.math.BigDecimal;

import java.time.LocalDate;

public class Developer {

String name;

BigDecimal salary;

LocalDate start;

public Developer(String name) {

this.name = name;

}

// get, set, constructor, toString

//...

}

控制台输出:

Developer{name='default', salary=1, start=2017-08-08}

Developer{name='mkyong', salary=1, start=2017-08-08}

supplier java8_Java 8之 Supplier示例相关推荐

  1. 答题卡的计分方式_如何建立信用风险模型和记分卡

    答题卡的计分方式 We are all aware of, and keep track of, our credit scores, don't we? That all-important num ...

  2. Supplier示例

    Supplier示例 在Java 8中,Supplier是一个函数接口,不需要接收参数,返回一个结果 @FunctionalInterface public interface Supplier< ...

  3. 【Java 进阶】匿名类(代码传递、回调、过滤器)、Lambda表达式(方法引用)、函数式接口(Supplier、Consumer、Predicate、Function)

    匿名类 匿名类(Anonymous Class) 匿名类的使用注意 匿名类 - 代码传递 - 测试代码运行时间的工具类 匿名类 - 回调 - 简易网络请求 匿名类 - 过滤器 - 获取目录下的所有文件 ...

  4. 函数式接口Supplier的用法

    文章目录 前言 supplier接口定义 具体使用 前言 最近看到公司写的rpc框架中,运用到了大量的函数式接口Supplier,下面将对supplier接口的具体使用简单介绍. supplier接口 ...

  5. Supplier接口

    目录 一.Supplier接口源码 二.Supplier示例 三.其他Supplier接口 一.Supplier接口源码 public interface Supplier<T> {/** ...

  6. 面试又挂了,你理解了 Java 8 的 Consumer、Supplier、Predicate和Function吗?

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 >>广而告之:打卡有奖活动火热进行中,快来参与吧,来了就有奖 今天我们还讲讲Consumer.Supplier ...

  7. Java8之Consumer、Supplier、Predicate和Function攻略

    今天我们还讲讲Consumer.Supplier.Predicate.Function这几个接口的用法,在 Java8 的用法当中,这几个接口虽然没有明目张胆的使用,但是,却是润物细无声的.为什么这么 ...

  8. java suppliers_java8的Supplier

    TOC java8的Supplier java.util.function.Supplier 定义 supplier也是是用来创建对象的,但是不同于传统的创建对象语法:new,每次调用get()方法时 ...

  9. Java 8之函数式编程(Function、Consumer、Supplier、Predicate)

    1.Function 定义 public interface Function <T, R> Represents a function that accepts one argument ...

最新文章

  1. php自动配置ip,使用PHP自动PING IP
  2. sqlserver中编写自定义函数中的返回值问题
  3. 面试智力题:天平称球
  4. HP前女老板Dunn和Carly的琐碎事
  5. Tomcat控制台输出到文件
  6. 吴恩达机器学习笔记二之多变量线性回归
  7. 智能硬件网络配置方式
  8. SPSS数据分析之描述性统计、区间估计与假设检验【操作详解】
  9. python zip用法_Python zip()用法及代码示例
  10. 安卓日记本设计内容介绍_天一 | 教你捷径,使用VB开发安卓程序!
  11. excel粘贴时出现故障_Excel常见问题及解决办法汇总
  12. Redis是否存在线程安全问题
  13. 实现微信公众号自定义分享功能,分享给朋友,分享到朋友圈,点击链接,获取点击分享者的openid。
  14. Winhex的使用(慢慢更
  15. 修改andriod模拟器的IMEI,IMSI,手机号,SIM卡号
  16. 学计算机科学与技术会秃顶吗,学计算机真的会秃顶吗?听听过来人怎么说
  17. 7-3 IP地址转换
  18. 64匹马,8赛道,找出跑得最快的4匹马,至少比赛9场
  19. C语言函数: 字符串函数及模拟实现strtok()、strstr()、strerror()
  20. 【对未来机器人的畅想】

热门文章

  1. 自定义控件之Canvas图形绘制基础练习-青春痘笑脸^_^
  2. 淘宝/天猫获得淘宝店铺详情 API
  3. 从零搭建基于 Java 的服务器生产环境
  4. opendrive网盘 一个能免费直接外链的网盘
  5. c++输出中文乱码怎么办?
  6. 【BUGKU之ez_java_serialize】
  7. 接口对接常用加密方法
  8. 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案
  9. mysql表disable_[MySQL优化案例]系列 -- DISABLE/ENABLE KEYS的作用
  10. 【Python】Python中字符串格式化实现整数前面自动补0