简介
java的lambda表达式是一种简单的匿名函数,这个匿名函数中都是干货
,参数列表,lambda表达式的符号->
和逻辑代码
lambda的表达式的形式如下
//当只有一个参数时,可以省略小括号
//当代码逻辑只有一行时,可以省大括号
(parm1,parm2,小括号内为参数列表) -> {大括号内为逻辑代码}
lambda的形式
1,无参数无返回值
new Thread(() -> System.out.println("haha")).start();
2,无参有返回值
3,有参无返回值
List<String> list = Arrays.asList("a", "b", "c", "d");
//x是参数,当只有一个参数时,可以省略小括号,当只有一行逻辑代码时,也可省略大括号
//精简后的代码 list.stream().forEach(x-> System.out.println(x));
list.stream().forEach((x)->{ System.out.println(x);});
4,有参有返回值
List<String> list = Arrays.asList("a", "b", "c", "d");
Stream<String> stream = list.stream().sorted((x, y) -> x.compareTo(y));
5,只有一个参数,省略小括号
List<String> list = Arrays.asList("a", "b", "c", "d");
list.stream().forEach(x-> System.out.println(x) );
6,只有一行代码逻辑,省略大括号
new Thread(() -> System.out.println("haha")).start();
获取Stream对象的三种方式
1,通过集合获取
List<String> list = Arrays.asList("a", "b", "c", "d");
Stream<String> stream = list.stream();
2,从数组中获取
String[] strs = {"a", "b", "c", "d"};
Stream<String> stream = Arrays.stream(strs);
3,Stream中的静态方法of获取流
Stream<String> stream1 = Stream.of("aa", "bb", "cc");
java中的方法引用
方法的引用就是这个方法已经实现了函数式接口中的方法即此方法的格式和函数式接口中方法的格式相同,当我们需要写lambda表达式的时候,直接引用此方法就可以了。
因此称为方法的引用
常用四大函数式接口
写在前面
我们平时遇到的接口基本都是这四个或这四个的子类。
我们也可以声明自己的函数式接口,但是意义不大,除非我们需要对某个lambda表达式进行更细粒度的控制。
Function(功能)
接口中的方法R apply(T var1);
用于有参数有返回值的场景
Consumer(消耗者)
接口中的方法void accept(T var1);
用于有参数无返回值的场景
Supplier(供应者)
接口中的方法T get();
用于无参数有返回值的场景
Predicate(使取决于)
接口中的方法boolean test(T var1);
用于有参数,返回判断结果的场景
Stream对象的方法
filter 过滤
Stream
Stream<String> streamA = Stream.of("a", "b", "c", "d");
Stream<String> streamB = streamA.filter(x -> x != "b");
map 把一个值映射成另外一个值
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Stream<String> streamA = Stream.of("a", "b", "c", "d");
Stream<String> streamB = streamA.map(x -> x.toUpperCase());
mapToDouble
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
flatMap
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
// 将map集合中的list数据提取组合成新的流(从二维变成了一维)
String[] words = new String[]{"Hello", "World"};
List<String> a = Arrays.stream(words)
.map(word -> word.split(""))
.flatMap((item) -> {
System.out.println(item.getClass());
return Arrays.stream(item);
})
.collect(Collectors.toList());
System.out.println(a);
flatMapToInt
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
flatMapToLong
LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
flatMapToDouble
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);
distinct
Stream
Stream<String> stream = Stream.of("a", "a", "a", "b");
List<String> collect = stream.distinct().collect(Collectors.toList());
System.out.println(collect);
sorted
Stream
Stream<String> stream = Stream.of("a", "a", "a", "b");
List<String> collect1 = stream.sorted().collect(Collectors.toList());
sorted
Stream
Stream<String> stream = Stream.of("a", "a", "a", "b");
List<String> collect2 = stream.sorted((x, y) -> x.compareTo(y)).collect(Collectors.toList());
peek
Stream
limit
Stream
skip
Stream
Stream<String> stream = Stream.of("a", "a", "a", "b","fd","afg","4","u");
List<String> collect = stream.skip(4).collect(Collectors.toList());
System.out.println(collect);
forEach
void forEach(Consumer<? super T> action);
Stream<String> stream = Stream.of("a", "a", "a", "b","fd","afg","4","u");
stream.forEach(x->System.out.println(x));
forEachOrdered
void forEachOrdered(Consumer<? super T> action);
toArray
Object[] toArray();
toArray
A[] toArray(IntFunction<A[]> generator);
reduce
T reduce(T identity, BinaryOperator
reduce
Optional
reduce
U reduce(U identity,
BiFunction<U, ? super T, U> accumulator,
BinaryOperator combiner);
collect
BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner);
collect
<R, A> R collect(Collector<? super T, A, R> collector);
Stream<String> stream = Stream.of("a", "a", "a", "b");
List<String> collect = stream.distinct().collect(Collectors.toList());
System.out.println(collect);
min
Optional
Stream<String> stream = Stream.of("a", "a", "a", "b","fd","afg","4","u");
Optional<String> min = stream.min((x, y) -> x.compareTo(y));
System.out.println(min.get());
max
Optional
Stream<String> stream = Stream.of("a", "a", "a", "b","fd","afg","4","u");
Optional<String> min = stream.max((x, y) -> x.compareTo(y));
System.out.println(min.get());
count
long count();
Stream<String> stream = Stream.of("a", "a", "a", "b");
long count = stream.count();
anyMatch
boolean anyMatch(Predicate<? super T> predicate);
allMatch
boolean allMatch(Predicate<? super T> predicate);
noneMatch
boolean noneMatch(Predicate<? super T> predicate);
findFirst
Optional
findAny
Optional
builder
public static
return new Streams.StreamBuilderImpl<>();
}
empty
public static
Stream<Object> empty = Stream.empty();
of
public static
of
@SafeVarargs
@SuppressWarnings("varargs") // Creating a stream from an array is safe
public static
Stream<String> a = Stream.of("a", "b", "c");
concat
public static
Objects.requireNonNull(a);
Objects.requireNonNull(b);
@SuppressWarnings("unchecked")
Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
(Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
return stream.onClose(Streams.composedClose(a, b));
}