JAVA四大核心函数式接口

Consumer接口

1.接口说明
Consumer接口是消费性接口,无返回值。Java8中对Consumer的定义如下所示。

@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); };
    }
}

2.使用示例

public void handlerConsumer(Integer number, Consumer<Integer> consumer){
    consumer.accept(number);
}
@Test
public void test1(){
    this.handlerConsumer(10000, (i) -> System.out.println(i));
}

Supplier接口

1.接口说明
Supplier接口是供给型接口,有返回值,Java8中对Supplier接口的定义如下所示。

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

2.使用示例

public List<Integer> getNumberList(int num, Supplier<Integer> supplier){
    List<Integer> list = new ArrayList<>();
    for(int i = 0; i < num; i++){
        list.add(supplier.get())
    }
    return list;
}

@Test
public void test2(){
    List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100));
    numberList.stream().forEach(System.out::println);
}

Function接口

1.接口说明
Function接口是函数型接口,有返回值,Java8中对Function接口的定义如下所示。

@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;
    }
}

2.使用示例

public String handlerString(String str, Function<String, String> func){
   return func.apply(str);
}

@Test
public void test3(){
   String str = this.handlerString("binghe", (s) -> s.toUpperCase());
   System.out.println(str);
}

Predicate接口

1.接口说明
Predicate接口是断言型接口,返回值类型为boolean,Java8中对Predicate接口的定义如下所示。

@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);
    }
}

2.使用示例

public List<String> filterString(List<String> list, Predicate<String> predicate){
    List<String> strList = new ArrayList<>();
    for(String str : list){
        if(predicate.test(str)){
            strList.add(str);
        }
    }
    return strList;
}

@Test
public void test4(){
    List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World");
    List<String> strList = this.filterString(list, (s) -> s.length() >= 5);
    strList.stream().forEach(System.out::println);
}
posted @ 2022-11-11 15:40  DurianTRY  阅读(56)  评论(0)    收藏  举报