Java8 TabTan
2020年11月1日记的笔记,迁移至此分享给需要他的人。主要介绍了java8的新特性,包含Lambda表达式、函数式接口、Stream流。
简介
oracle于2014年3月发布,java5以来最具革命性地版本。
Lambda表达式
语法
格式一:有参数,没有返回值
1.举例 (o1,o2) -> Integer.compare(o1,o2);
2.格式
- -> :lambda操作符 或 箭头操作符
- -> 的左边 :lambda形参列表(就是接口中抽象方法的形参列表)
- -> 的右边 :lambda体 (就是重写的抽象方法的方法体)
3.Lambda表达式的使用
@Test
public void test1(){
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("666");
}
};
r.run();
System.out.println("*************");
Runnable r1 = ()-> System.out.println("Lambda 666");
r1.run();
}
-------------------------------------------------------------------
666
*************
Lambda 666
4.lambda表达式的本质:作为函数式接口的实例
格式二:需要一个参数,没有返回值
格式三:数据类型可以省略,因为可由编译器推断出,称为“类型推断”
@Test
public void test3(){
Consumer<String> con = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
con.accept("我是老方法");
System.out.println("*************");
Consumer<String> con1 = (s)-> System.out.println(s);
con1.accept("我是lambda");
}
--------------------------------------------------------------
我是老方法
*************
我是lambda
格式四:Lambda若只需要一个参数时,参数的小括号可以省略
@Test
public void test4(){
Consumer<String> con1 = s-> System.out.println(s);
con1.accept("我是lambda");
}
---------------------------------------------------------------
我是lambda
格式五:lambda需要两个或以上的参数,多条执行语句,并且可以有返回值
@Test
public void test5(){
Comparator<Integer> c = (o1,o2) ->{
System.out.println(o1);
System.out.println(o2);
return o1-o2;
};
System.out.println(c.compare(1, 2));
}
---------------------------------------------------------------
1
2
-1
格式六:当lambda体只有一条语句时,return与大括号若有,都可以省略 (略)
总结
- -> 左边:lambda 形参列表的参数类型可以省略(类型推断),如果lambda形参列表只有一个参数,其一对()也可以省略
- -> 右边:lambda 体由 { } 包裹,如果 lambda 体只有一条语句可以省略 { } 和 return 关键字
函数式(Functional)接口
简介
- 只包含一个抽象方法的接口,称为函数式接口。
- 在一个接口上使用 @FunctionalInterface 注解检测这个接口是否是函数式接口。
- java.util.function 定义了很多函数式接口
四大函数式接口
Java内置四大核心函数式接口
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
Consumer<T>消费型接口 | T | void | 对类型为T的对象应用操作,包含方法:void accept(T t) |
Supplier<T>供给型接口 | 无 | T | 返回类型为T的对象,包含方: T get() |
Function<T,R>函数型接口 | T | R | 对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方法:R apply(T t) |
Predicate<T>断定型接口 | T | boolean | 确定类型为T的对象是否满足某种约束,并返回boolean值,包含方法:boolean test(T t) |
举例
@Test
public void test1(){
happyTime(500,money -> System.out.println("消费"+money+"元,芜湖,起飞!"));
}
public void happyTime(double money, Consumer<Double> consumer){
consumer.accept(money);
}
------------------------------------------------------------------------------------
消费500.0元,芜湖,起飞!
@Test
public void test2(){
ArrayList<String> strings = new ArrayList<>();
strings.add("aaaaaaaaaaaaa");
strings.add("bbbbbbbbbbbbbbb");
strings.add("a");
strings.add("b");
List<String> list = filterString(strings, s -> s.length() < 3);
for (String s : list) {
System.out.println(s);
}
}
public List<String> filterString(List<String> list, Predicate<String> predicate){
ArrayList<String> filterList = new ArrayList<>(list.size());
for (String s:list) {
if (predicate.test(s)){
filterList.add(s);
}
}
return filterList;
}
---------------------------------------------------------------------------------------
a
b
方法引用与构造器引用
简介
- 当要传递给lambda体的操作,已经有实现的方法了,就可以使用方法引用。
- 方法引用就是lambda表达式,通过方法名来指向一个方法,可以认为是 lambda 的语法糖。
语法
使用格式:类(或对象):: 方法名
具体分为如下三种情况
- 对象 :: 非静态方法
- 类 :: 静态方法
- 类 :: 非静态方法
情况一 对象 :: 实例方法
@Test
public void test3(){
Consumer<String> con = System.out::println;
con.accept("方法引用,起飞,起飞");
}
------------------------------------------------------
方法引用,起飞,起飞
注意
方法引用使用时要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值类型相同。 (情况三除外)
情况二 类 :: 静态方法
@Test
public void test4(){
Comparator<Integer> con = Integer::compare;
System.out.println(con.compare(1, 2));
}
-------------------------------------------------
-1
情况三 类 :: 实例方法
@Test
public void test5(){
Comparator<String> com = (s1,s2) -> s1.compareTo(s2);
System.out.println(com.compare("a","b"));
System.out.println("***********************");
Comparator<String> com1 = String::compareTo;
System.out.println(com1.compare("b","c"));
}
--------------------------------------------------
-1
***********************
-1
构造器引用
@Test
public void test6(){
Supplier<Student> sup = () -> new Student();
System.out.println(sup.get());
System.out.println("****************");
Supplier<Student> sup1 = Student::new;
System.out.println(sup1.get());
}
数组引用
@Test
public void test7(){
Function<Integer,String[]> fun = length -> new String[length];
String[] apply = fun.apply(3);
System.out.println(apply.length);
Function<Integer,String[]> fun1 = String[]::new;
String[] apply1 = fun1.apply(2);
System.out.println(apply1.length);
}
---------------------------------------------------------------------
3
2
强大的Stream API
Stram的实例化
创建一 :通过调用Collection的接口默认方法 stream()
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
创建二:通过数组
数组工具类Arrays的stream方法,例如
@Test
public void test(){
int[] arr = {1,2,3,4};
IntStream stream = Arrays.stream(arr);
}
创建三:Stream 的 of() 方法,例如
@Test
public void test2(){
Stream<Integer> integerStream = Stream.of(1, 2, 3);
Stream<String> stringStream = Stream.of("123");
}
创建四:创建无限流
Stream.iterate()
Stream的中间操作
筛选与切片
filter
@Test
public void test3(){
List<People> list = new ArrayList<>();
list.add(new People("张三",17));
list.add(new People("李四",22));
list.add(new People("王五",30));
list.add(new People("Tab Tan",18));
Stream<People> stream = list.stream();
Stream<People> peopleStream = stream.filter(p -> p.getAge() > 20);
peopleStream.forEach(System.out::println);
}
------------------------------------------------------------------------
People{name='李四', age=22}
People{name='王五', age=30}
limit
@Test
public void test4(){
List<People> list = new ArrayList<>();
list.add(new People("张三",17));
list.add(new People("李四",22));
list.add(new People("王五",30));
list.add(new People("Tab Tan",18));
Stream<People> stream = list.stream();
Stream<People> limit = stream.limit(3);
limit.forEach(System.out::println);
}
---------------------------------------------------------------------------
People{name='张三', age=17}
People{name='李四', age=22}
People{name='王五', age=30}
skip
@Test
public void test5(){
List<People> list = new ArrayList<>();
list.add(new People("张三",17));
list.add(new People("李四",22));
list.add(new People("王五",30));
list.add(new People("Tab Tan",18));
Stream<People> stream = list.stream();
Stream<People> skip = stream.skip(2);
skip.forEach(System.out::println);
}
--------------------------------------------------------------------------
People{name='王五', age=30}
People{name='Tab Tan', age=18}
distinct
@Test
public void test6(){
List<People> list = new ArrayList<>();
list.add(new People("张三",17));
list.add(new People("李四",22));
list.add(new People("王五",30));
list.add(new People("Tab Tan",18));
Stream<People> stream = list.stream();
// 去重
Stream<People> distinct = stream.distinct();
distinct.forEach(System.out::println);
}
----------------------------------------------------------------------------
People{name='张三', age=17}
People{name='李四', age=22}
People{name='王五', age=30}
People{name='Tab Tan', age=18}
映射
map(Function f)
@Test
public void test7(){
List<String> list = Arrays.asList("a", "B", "C");
Stream<String> stream = list.stream();
Stream<String> stringStream = stream.map(String::toUpperCase);
stringStream.forEach(System.out::println);
}
-------------------------------------------------------------------------------
A
B
C
mapToDouble
mapToInt
mapToLong
flatMap
排序
自然排序sorted
@Test
public void test8(){
List<String> list = Arrays.asList("11", "22", "44", "33");
list.stream().sorted().forEach(System.out::println);
}
---------------------------------------------------------------------------
11
22
33
44
定制排序sorted (Comparator com)
@Test
public void test9(){
Comparator<People> comparator = Comparator.comparingInt(People::getAge);
List<People> list = new ArrayList<>();
list.add(new People("张三",17));
list.add(new People("李四",22));
list.add(new People("王五",30));
list.add(new People("Tab Tan",18));
Stream<People> stream = list.stream();
stream.sorted(comparator).forEach(System.out::println);
}
-----------------------------------------------------------------------------
People{name='张三', age=17}
People{name='Tab Tan', age=18}
People{name='李四', age=22}
People{name='王五', age=30}
终止操作
匹配与查找
allmatch(Predicate p) 检查是否匹配所有元素
anyMatch(Predicate p) 至少一个
@Test
public void test10(){
Comparator<People> comparator = Comparator.comparingInt(People::getAge);
List<People> list = new ArrayList<>();
list.add(new People("张三",17));
list.add(new People("李四",22));
list.add(new People("王五",30));
list.add(new People("Tab Tan",18));
Stream<People> stream = list.stream();
boolean b = stream.anyMatch((e) -> e.getAge() < 20);
System.out.println(b);
}
-------------------------------------------------------------------------------------
true
noneMatch(Predicate p) 没有
findFirst 返回第一个元素
findAny 返回当前流中的任意元素
count 返回流元素的总个数
max(Comparator c) 返回流中最大值
min(Comparator c) 返回流中最小值
forEach 懂的都懂
规约
reduce(T iden,BinaryOperator b)
可以将流中元素反复结合起来,得到一个值。返回T
@Test
public void test11(){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
Integer reduce = stream.reduce(0, Integer::sum);
System.out.println(reduce);
}
-------------------------------------------------------------------------------------
15
reduce(BinaryOperator b)
可以将流中元素反复结合起来,得到一个值,返回Optional<T>
@Test
public void test12(){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
Optional<Integer> reduce = stream.reduce( Integer::sum);
System.out.println(reduce);
}
-------------------------------------------------------------------------------------
Optional[15]
收集
collect(cellector c)
将流转换为其他形式,接收一个Collector接口实现,用于给Stream中元素做汇总的方法
@Test
public void test13(){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
List<Integer> collect = stream.skip(2).collect(Collectors.toList());
collect.forEach(System.out::println);
}
-------------------------------------------------------------------------------------
3
4
5
Optional类
就是个容器,把你的东西给包着。