• JAVA中Consumer接口的用法
  • 发布于 2个月前
  • 162 热度
    0 评论
Java 8 引入了 java.util.function 包,其中包含了一些常用的函数式接口,如 Consumer、Function、Predicate 等。Consumer 接口是其中一个函数式接口,用于表示接受一个输入参数并执行某种操作的操作者。它包含一个抽象方法 accept(T t),该方法接受一个泛型类型的参数 T,并且没有返回值。
源码
package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}
@FunctionalInterface是一个标记注解,该注解并非强制性的,但它可以帮助编译器检查是否符合函数式接口的定义。
以下是如何在 Java 中使用 Consumer 接口的一些建议:
1. 基本用法
import java.util.function.Consumer;

public class ConsumerExample {

    public static void main(String[] args) {
        // 创建一个Consumer实例
        Consumer<String> myConsumer = s -> System.out.println(s);

        // 使用Consumer的accept方法
        myConsumer.accept("Hello, Consumer!");
    }
}
2. 组合多个 Consumer:
你可以使用 andThen 方法将多个 Consumer 组合在一起,形成一个串行执行的操作链。
import java.util.function.Consumer;

public class ConsumerExample {

    public static void main(String[] args) {
        // 创建两个Consumer实例
        Consumer<String> printUpperCase = s -> System.out.println(s.toUpperCase());
        Consumer<String> printLength = s -> System.out.println("Length: " + s.length());
        // 堆代码 duidaima.com
        // 组合两个Consumer
        Consumer<String> combinedConsumer = printUpperCase.andThen(printLength);

        // 使用组合后的Consumer
        combinedConsumer.accept("Hello, Consumer!");
    }
}
3. 使用方法引用:
你可以使用方法引用来引用已有的方法,从而创建 Consumer 的实例。
import java.util.function.Consumer;
public class ConsumerExample {

    public static void printUpperCase(String s) {
        System.out.println(s.toUpperCase());
    }

    public static void main(String[] args) {
        // 使用静态方法引用创建Consumer实例
        Consumer<String> myConsumer = ConsumerExample::printUpperCase;

        // 使用Consumer的accept方法
        myConsumer.accept("Hello, Consumer!");
    }
}
4. 在集合操作中使用:
Consumer 接口在 Java 8 引入的函数式编程特性中,经常与集合框架的方法结合使用,例如 forEach 方法:
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerExample {

    public static void main(String[] args) {
        List<String> words = Arrays.asList("Java", "is", "fun");

        // 使用Consumer的forEach方法遍历集合元素
        Consumer<String> printUpperCase = s -> System.out.println(s.toUpperCase());
        words.forEach(printUpperCase);
    }
}
这些示例展示了如何使用 Consumer 接口执行各种操作,包括基本用法、组合、方法引用以及集合操作中的应用。
用户评论