@FunctionalInterfacepublic interface Supplier { /** * Gets a result. * * @return a result */ T get();}
02
深度解析如下列代码所示:使用Supplier创建对象,语法结构:
无参数:
1. Supplier instance = T::new;
2. Supplier instance = () -> new T();
有参数:
1. Functionfun = T::new; fun.apply("test");2. Function fun2 = str -> new T(str); fun2.apply("test2");
注:
每次调用get方法都会创建一个对象,下面的代码中调用了两次get方法,打印的hashcode是不一样的!
public class TestSupplier { public static void main(String[] args) { //无参数1: Supplier sup = TestSupplier::new; sup.get(); sup.get(); //无参数2: Supplier sup2 = () -> new TestSupplier(); sup2.get(); sup2.get(); //有参数1: Functionfun = TestSupplier::new; fun.apply("test"); //有参数2: Function fun2 = str -> new TestSupplier(str); fun2.apply("test2"); } public TestSupplier() { System.out.println(this.hashCode()); } public TestSupplier(String str) { System.out.println(this.hashCode() + ",参数:" + str); }}
java新特性文章:
● java新特性之——这些Java9的新特性你用过几个?
● java新特性之——面试官老爱问的JAVA8 新特性
架构系列文章:
● 架构系列——限流算法介绍
● 架构系列——volatile的可见性、防止指令重排序以及不能保证原子性
● 架构系列——使用synchronized需要注意什么细节
● 架构系列——线程实现方式以及生命周期的探索
● 架构系列——并发、并行与多线程关系探索
● 架构系列——单体、分布式、集群与冗余的探索
● 架构系列——Java反射:框架设计的灵魂
面试系列文章:
● 所有程序员会面临的问题:程序员只能吃青春饭?
● 要准备多少东西去面试---java中高级面试总结(值得收藏)
● 最近的面试有感(7个方面)
![63e046ded9ea4990946045be6f39e7a1.png](https://i-blog.csdnimg.cn/blog_migrate/934c451618cb2b053fceba6e24302e21.png)