Loading

java8之函数接口

函数接口

image-20210316112745994

Predicate

传入的是一个lambda,返回的布尔值

一个入参

public interface Predicate<T>

boolean test(T t);

这个和stream的filter很像

List<Apple> greenList = filter(list, (apple) -> apple.getColor().equals("green"));

private static List<Apple> filter(List<Apple> source, Predicate<Apple> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple a : source) {
            if (predicate.test(a))
                result.add(a);
        }
        return result;

    }

两个入参

public interface BiPredicate<T, U> 

boolean test(T t, U u);
 List<Apple> result2 = filterByBiPredicate(list, (s, w) -> s.equals("green") && w > 100);   

private static List<Apple> filterByBiPredicate(List<Apple> source, BiPredicate<String, Long> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple a : source) {
            if (predicate.test(a.getColor(), a.getWeight()))
                result.add(a);
        }
        return result;
    }

Consumer

接受参数,中间lambda对参数进行处理输出结果,不返回值

public interface Consumer<T> 
void accept(T t);

一个参数

simpleTestConsumer(list, a -> System.out.println(a));

private static void simpleTestConsumer(List<Apple> source, Consumer<Apple> consumer) {
        for (Apple a : source) {
            consumer.accept(a);
        }
    }

两个参数

public interface BiConsumer<T, U> 
void accept(T t, U u);    
simpleBiConsumer("XXX", list, (a, s) -> System.out.println(s + a.getColor() + ":Weight=>" + a.getWeight()));

private static void simpleBiConsumer(String c, List<Apple> source, BiConsumer<Apple, String> consumer) {
        for (Apple a : source) {
            consumer.accept(a, c);
        }
    }

Function

两个参数

两个参数,一个作为输入源,一个是处理后的输出结果

public interface Function<T, R> 

R apply(T t);
String result3 = testFunction(new Apple("yellow", 100), (a) -> a.toString());
    
private static String testFunction(Apple apple, Function<Apple, String> fun) {
        return fun.apply(apple);
    }

三个参数

public interface BiFunction<T, U, R>
R apply(T t, U u);
Apple a = testBiFunction("Blue", 130, (s, w) -> new Apple(s, w));

private static Apple testBiFunction(String color, long weight, BiFunction<String, Long, Apple> fun) {
        return fun.apply(color, weight);
    }

IntFunction

输入一个int值,对int进行操作返回r类型


public interface IntFunction<R>

R apply(int value)
        IntFunction<Double> f = i -> i * 100.0d;
        double result4 = f.apply(10);

BiFunction

输入T, U,操作返回 R

public interface BiFunction<T, U, R>
R apply(T t, U u);    
Apple a = testBiFunction("Blue", 130, (s, w) -> new Apple(s, w));

private static Apple testBiFunction(String color, long weight, BiFunction<String, Long, Apple> fun) {
        return fun.apply(color, weight);
    }

Supplier

public interface Supplier<T>

T get()

这里有一个函数推导的概念

Supplier<String> s = String::new;   //method inference.
System.out.println(s.get().getClass());
    Apple a2 = createApple(() -> new Apple("Green", 100));
    private static Apple createApple(Supplier<Apple> supplier) {
        return supplier.get();
    }
posted @ 2021-03-16 11:29  candidcrat  阅读(59)  评论(0)    收藏  举报