Java学习网站 logicbig
捕获.PNG
图片来自 java 8 Stream Tutoriala
如图 蓝色区域是流的创建,包括数组、文件、集合等;淡黄色区域是流的中间操作,包括过滤、排序、映射、扁平化等;绿色区域是流的终端操作,包括分组、收集、统计、归约等。
捕获.PNG
介绍下几个常用的函数接口
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
@FunctionalInterface
public interface Supplier<T> {
T get();
}
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
Predicate
的运用,可以组合条件筛选数据,可以去重等
public class Test {
static Predicate<String>
p1 = s -> s.contains("bar"),
p2 = s -> s.length() < 5,
p3 = s -> s.contains("foo"),
p4 = p1.negate().and(p2).or(p3);
public static void main(String[] args) {
Stream.of("bar","foobar","barBaz").filter(p4).forEach(System.out::println); // 打印 foobar
}
}
public class Test {
static List<Bean> list = Lists.newArrayList(new Bean("a"),new Bean("a"),new Bean("c"));
@Data
static class Bean {
@NonNull
String name;
}
static <T> Predicate<T> distinct(Function<? super T,?> function) {
Set<Object> set = new HashSet<>();
return t -> set.add(function.apply(t));
}
public static void main(String[] args) {
list.stream().filter(distinct(Bean::getName)).forEach(System.out::println);
}
}
Function
的运用,如求正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积function(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
,斐波那契数列 0、1、1、2、3、5、8、13、21、34
,柯里化等
public class Test {
static Function<Integer, Integer> function;
public static void main(String[] args) {
function = n -> n == 0 ? 1 : n * function.apply(n - 1);
IntStream.range(0,10).forEach(i -> System.out.println(function.apply(i)));
}
}
public class Test {
static Function<Integer, Integer> function;
public static void main(String[] args) {
function = n -> n == 0 ? 0 : n == 1 ? 1 : function.apply(n - 1) + function.apply(n - 2);
IntStream.range(0,10).forEach(i -> System.out.println(function.apply(i)));
}
}
public class Test {
static Function<String, String> f1 = s -> s.toLowerCase(),
f2 = s -> s.replaceAll("a","b"),f3 = f1.andThen(f2),f4= f1.compose(f2);
public static void main(String[] args) {
System.out.println(f3.apply("A")); // a
System.out.println(f4.apply("A")); // b
}
}
public class Test {
static Function<String, Function<String,String>> f = a -> b -> a + b;
public static void main(String[] args) {
System.out.println(f.apply("hello").apply("java")); // hello java
}
}
Supplier
的运用,如反射实例化对象
public class MySupplier<T> implements Supplier<T> {
private Class<T> type;
public MySupplier(Class<T> type) {
this.type = type;
}
@Override
@SneakyThrows
public T get() {
return type.newInstance();
}
public static <T> Supplier<T> newInstance(Class<T> type) {
return new MySupplier<>(type);
}
public static void main(String[] args) {
Bean bean = newInstance(Bean.class).get(); // 无参构造
}
}
@Data
class Bean {
String name;
}
public class Test implements Supplier<Integer>{
private int item;
@Override
public Integer get() {
return item++ ;
}
public static void main(String[] args) {
Stream.generate(new Test()).limit(10).forEach(System.out::println); // 0~9
}
}