List<String> lowCaloricDishesName = menu.parallelStream() .filter(d -> d.getCalories() < 400) .sorted(comparing(Dishes::getCalories)) .map(Dish::getName) .collect(toList()); Map<Dish.Type, List<Dish>> dishesByType = menu.stream().collect(groupingBy(Dish::getType));
BiFunction<String, String,String> biFunction = (x, y) -> { return x+"==="+y; }; Function<String,String> fun = x ->x + " after8"; System.out.println(biFunction.andThen(fun).apply("tpyyes.com ", " java8"));
函数式接口 函数描述符 原始类型特化
Predicate<T> T->boolean IntPredicate,LongPredicate, DoublePredicate
Consumer<T> T->void IntConsumer,LongConsumer, DoubleConsumer
Function<T,R> T->R IntFunction<R>,
IntToDoubleFunction,
IntToLongFunction,
LongFunction<R>,
LongToDoubleFunction,
LongToIntFunction,
DoubleFunction<R>,
ToIntFunction<T>,
ToDoubleFunction<T>,
ToLongFunction<T>
Supplier<T> ()->T BooleanSupplier,IntSupplier, LongSupplier,
DoubleSupplier
UnaryOperator<T> T->T IntUnaryOperator,
LongUnaryOperator,
DoubleUnaryOperator
BinaryOperator<T> (T,T)->T IntBinaryOperator,
LongBinaryOperator,
DoubleBinaryOperator
BiPredicate<L,R> (L,R)->boolean
BiConsumer<T,U> (T,U)->void ObjIntConsumer<T>,
ObjLongConsumer<T>,
ObjDoubleConsumer<T>
BiFunction<T,U,R> (T,U)->R ToIntBiFunction<T,U>,
ToLongBiFunction<T,U>,
ToDoubleBiFunction<T,U>
表4-1 中间操作
操 作 类 型 返回类型 操作参数 函数描述符
filter 中间 Stream<T> Predicate<T> T -> boolean
map 中间 Stream<R> Function<T, R> T -> R
limit 中间 Stream<T>
sorted 中间 Stream<T> Comparator<T> (T, T) -> int
distinct 中间 Stream<T>
表4-2 终端操作
操 作 类 型 目 的
forEach 终端 消费流中的每个元素并对其应用Lambda。这一操作返回void
count 终端 返回流中元素的个数。这一操作返回long
collect 终端 把流归约成一个集合,比如List、Map 甚至是Integer。详见第6 章