目录

[TOC]

lambda

lambda表达式语法:参数列表 + 箭头 + 方法体

简化写法:

  • 参数类型可以不写,只写(参数名),参数变量名随意定义;
  • 参数表最少可以只有一个 (),或者只有一个参数名;
  • 方法体如果只有一句话,{} 可以省略

总结:

  • Lambda表达式: (参数表) -> {方法体}

  • 分辨出你的接口是否函数式接口。 函数式接口就可以lambda简化

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 自定义函数式接口
*/
@FunctionalInterface//用于检查该接口是否是函数式接口
interface LambdaInterfaceTest {
void sum(int a, int b);
}

public class LambdaTest {

public static void main(String[] args) {
LambdaInterfaceTest lambdaInterfaceTest = (a, b) -> System.out.println("求和:a+b=" + (a + b));
lambdaInterfaceTest.sum(1, 2);
}
}

Function

Java内置function

BiConsumer IntPredicate ToDoubleBiFunction
BiFunction IntSupplier ToDoubleFunction
BinaryOperator IntToDoubleFunction ToIntBiFunction
BiPredicate IntToLongFunction ToIntFunction
BooleanSupplier IntUnaryOperator ToLongBiFunction
Consumer LongBinaryOperator ToLongFunction
DoubleBinaryOperator LongConsumer UnaryOperator
DoubleConsumer LongFunction
DoubleFunction LongPredicate
DoublePredicate LongSupplier
DoubleSupplier LongToDoubleFunction
DoubleToIntFunction LongToIntFunction
DoubleToLongFunction LongUnaryOperator
DoubleUnaryOperator ObjDoubleConsumer
Function ObjIntConsumer
IntBinaryOperator ObjLongConsumer
IntConsumer Predicate
IntFunction Supplier

函数式接口的出入参定义:

1、有入参,无出参【消费者】:function.accept

1
2
3
4
BiConsumer<String,String> function = (a,b)->{ //能接受两个入参
System.out.println("哈哈:"+a+";呵呵:"+b);
};
function.accept("1","2");

有入参,有出参【多功能函数】:function.apply

1
2
Function<String,Integer> function = (String x) -> Integer.parseInt(x);
System.out.println(function.apply("2"));

3、无入参,无出参【普通函数】:

1
2
3
Runnable runnable = () -> System.out.println("aaa");

new Thread(runnable).start();

4、无入参 ,有出参【提供者】:supplier.get()

1
2
3
Supplier<String> supplier = ()-> UUID.randomUUID().toString();
String s = supplier.get();
System.out.println(s);

java.util.function包下的所有function定义:

  • Consumer: 消费者
  • Supplier: 提供者
  • Predicate: 断言

get/test/apply/accept调用的函数方法;

Stream API

Stream所有数据和操作被组合成流管道流管道组成:

  • 一个数据源(可以是一个数组、集合、生成器函数、I/O管道)
  • 零或多个中间操作(将一个流变形成另一个流)
  • 一个终止操作(产生最终结果)

创建流

  • of、builder、empty、ofNullable、generate、concat、集合.stream

中间操作(Intermediate Operations)

  • filter
  • map、mapToInt、mapToLong、mapToDouble
  • flatMap、flatMapToInt、flatMapToLong、flatMapToDouble
  • mapMulti、mapMultiToInt、mapMultiToLong、mapMultiToDouble
  • parallel、unordered、onClose、sequential
  • distinctsortedpeek、limit、skip、takeWhile、dropWhile

终止操作(Terminal Operation)

  • forEach、forEachOrdered、toArray、reducecollect、toList、min、max、countanyMatch、allMatch、noneMatch、findFirstfindAny、iterator

详细解释

filter:过滤流中的元素,只保留满足给定条件的元素。

map、mapToInt、mapToLong、mapToDouble:将流中的每个元素转换为另一种类型或执行某种操作。

flatMap、flatMapToInt、flatMapToLong、flatMapToDouble:将流中的每个元素转换为另一个流,然后将这些流连接在一起。

mapMulti、mapMultiToInt、mapMultiToLong、mapMultiToDouble:与map类似,但允许多个输入元素映射到相同的输出元素。

parallel、unordered、onClose、sequential:控制流的并行处理方式。

distinct:去除流中的重复元素。

sorted:对流中的元素进行排序。

peek:对流中的每个元素执行操作,但不改变流中的元素。

limit、skip、takeWhile、dropWhile:限制流中的元素数量,或者跳过和保留满足特定条件的元素。

forEach、forEachOrdered:对流中的每个元素执行操作,但不返回任何结果。

toArray:将流中的元素收集到一个数组中。

reduce:将流中的元素通过一个二元操作符组合成一个值。

collect:将流中的元素收集到一个集合中。

toList:将流中的元素收集到一个列表中。

min、max:找到流中的最小值和最大值。

count:计算流中的元素数量。

anyMatch、allMatch、noneMatch:检查流中是否有任何元素满足给定的条件。

findFirst、findAny:查找流中的第一个或任意一个元素。

iterator:获取流的迭代器。