Java8基础知识点梳理
最近可能是自己的原因导致对Java8的学习落入的下乘,我们就来抽个时间将Java8的知识点进行梳理下吧
常用的函数(函数式接口)
-
1.function
根据我的记忆是function里面有个apply方法,apply方法是接受一个参数,并不返回值,我们去看下源码
那么就比较明显了,我记错了,在我看来这个是一个好事,下面我们去写个代码去测试一下这个方法
import java.util.function.Function;
/**
* @author Zerox
* @date 2018/11/30 8:58
*
* 目的:测试Functinon里面apply函数的作用
*
* 该apply(T t)方法给出的注释是将给定的参数应用到这个函数中
* 通俗的来将就是传参,不过这个参数传递的是一个方法
*/
public class TestFunction {
public static void main(String[] args) {
int length = TestFunction("hello",stringValue -> stringValue.length());
System.out.println(length);
}
/**
*
* @param strValue 输入的参数
* @param function 调用apply方法
* @return
*/
public static int TestFunction(String strValue ,Function<String,Integer> function) {
return function.apply(strValue);
}
}
函数的运行结果是:
- 2.supplier函数
先说下自己的感受吧,首先咋一开始的时候,我对这个方法是毫无波动的,为什么呢?可能理解的比较浅吧,现在我的内心也是毫无波动的,只是认为单纯的生成一个对象根据所提供的泛型,唯一的联想是可能这个东西类似于工厂模式吧,接着我们去看下源码
这个方法看来我并没有记错,我们去阅读下这个方法到底什么意思吧一句话"结果的集装箱",返回一个新的或者"集装箱里面不存在的,我们测试下它说的意思"
import java.util.function.Supplier;
/**
* @author Zerox
* @date 2018/11/30 9:26
*/
public class TestSupplier {
public static void main(String[] args) {
/**
* 所遇到的问题:
* 1. 不会生成对象了 ---> 搞出来两种实现方法
* a. Supplier<StudentEntity> supplier = StudentEntity::new; //第一种实现方法
* b. Supplier<StudentEntity> supplier = () -> new StudentEntity(); // 第二种实现方法
*
* 本质上属于一种
*/
Supplier<StudentEntity> supplier =StudentEntity::new; //第一种实现方法
Supplier<StudentEntity> supplier1 =StudentEntity::new; //第一种实现方法
// 判断,我们上面所说的不存在的对象或者新生成的对象
if (supplier == supplier1) { // 直接比较地址就好
System.out.println("1");
} else{
System.out.println("0");
}
// 结果确实是不存在的,新的。
}
}
函数的运行结果是:
-3.Consumer函数
如果,不出意外这个是我一开始,出意外的那个东西,不过,我思考错了,这个就是那种接受参数不返回值那个,我们来看一下源码
这个方法就比较有意思了,有输入无返回值,我都不知道他是干嘛用的,我的理解是:它只能够等同于System.out.println()这个输出语句了
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* @author Zerox
* @date 2018/11/30 10:47
*/
public class TestConsumer {
public static void main(String[] args) {
List<StudentEntity> list = new ArrayList<>();
Consumer<StudentEntity> consumer = x -> {
list.add(x);
};
StudentEntity studentEntity = new StudentEntity(15,"zhangsan");
StudentEntity studentEntity1 = new StudentEntity(16,"lisi");
StudentEntity studentEntity2 = new StudentEntity(17,"wangwu");
StudentEntity studentEntity3 = new StudentEntity(18,"zhaoliu");
Stream.of(studentEntity,studentEntity1,studentEntity2,studentEntity3).forEach(consumer);
list.stream().forEach(System.out::println);
}
/**
*
* @param stringValue 待操作的值
* @param consumer 操作函数
*
* 像我这样写是毫无意义的,才去百度了一下大神的简书 https://www.jianshu.com/p/0b955173045e
*/
public static void testConsumer(String stringValue,Consumer<String> consumer) {
consumer.accept(stringValue);
}
}
-3.BiFunction函数
这个函数可以说是Function函数的拓展吧,function函数appy()方法接受一个参数,返回一个参数,而Bifunction是接收两个参数,返回一个参数,下面我们来看下源码
我能想到的就是两个数字的操作,下面我们来看代码吧
import java.util.function.BiFunction;
/**
* @author Zerox
* @date 2018/11/30 13:11
*/
public class TestBiFunction {
public static void main(String[] args) {
System.out.println(TestBiFunction(3,4,(i,j) -> i + j));
System.out.println(TestBiFunction(3,4,(i,j) -> i - j));
System.out.println(TestBiFunction(3,4,(i,j) -> i * j));
System.out.println(TestBiFunction(3,4,(i,j) -> i / j));
}
/**
*
* @param i 待操作的数字1
* @param j 待操作的数字2
* @param biFunction 即将调用的函数式接口
* @return 传入的函数
*/
public static int TestBiFunction(int i,int j,BiFunction<Integer,Integer,Integer> biFunction) {
return biFunction.apply(i,j);
}
}
函数的运行结果是:
-4.Predicate函数
predicate函数这个函数比较有意思,在我的认知里面就是"判别式"这样子说,感觉不太好懂,换个说法就是你去试鞋子,你的脚需要把鞋子穿进去,那么这个鞋子的号码就是条件,下面我们来看下源代码
给定一个参数判断为true或者false
import java.util.function.Predicate;
/**
* @author Zerox
* @date 2018/11/30 13:44
*/
public class TestPredicate {
public static void main(String[] args) {
System.out.println(testPredicate("hello world",strvalue -> strvalue.equals("hello world")));
System.out.println("******");
System.out.println(testPredicate("hello world",strvalue -> strvalue.equals("hello world1")));
}
public static boolean testPredicate(String strvalue,Predicate predicate) {
return predicate.test(strvalue);
}
}
函数的运行结果是:
-4.Optional类
这个类需要说一下,因为她很重要,防止空指针操作,下面我们看下源码
这个方法是判断容器里面的对象是否为空,如果为空会返回什么呢?不为空又会怎样?我们去测试下,下面我们去写下测试代码
这个先不说了,需要拿出来单独去说,这个方法比较麻烦
存在问题,请指出,不胜感激