标签:sum ntp 一个 consumer 函数式 stream 例子 print and
Supplier<T> 返回T类型对象
T get();
Supplier<Apple> supplier = () -> new Apple();
// Supplier<Apple> supplier = Apple::new;
supplier.get();
Consumer<T> 接收一个 T 类型
void accept(T t);
List<String> languages = Arrays.asList("java","scala","python");
Consumer<String> consumer = System.out::println;
languages.stream().forEach(consumer);
Function<T, R> 由T类型对象转成R类型对象
R apply(T t);
List<String> languages = Arrays.asList("java","scala","python");
Function<String, Integer> ti = String::length;
languages.stream()
.map(ti)
.forEach(System.out::println);
Predicate<T> 条件判断
boolean test(T t);
List<String> languages = Arrays.asList("java","scala","python");
Predicate<String> p = (str) -> str.length() > 4;
languages.stream()
.filter(p)
.forEach(System.out::println);
List<String> languages = Arrays.asList("java","scala","python");
Function<String, Integer> ti = String::length;
BiConsumer<Integer, String> bi = (a, b) -> System.out.println(b + a);
IntPredicate ip = x -> x > 4;
IntPredicate ip1 = x -> x < 100;
IntUnaryOperator io = x -> x * x;
languages.stream()
.map(ti)
.map(io::applyAsInt)
.filter(x->ip.and(ip1).test(x))
.forEach(x -> bi.accept(x, "prefix_"));
部分函数式接口中有 default 方法, 可以进行组合使用!
标签:sum ntp 一个 consumer 函数式 stream 例子 print and
原文地址:https://www.cnblogs.com/wansw/p/10225928.html