企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 2.2.2 Consumer Consumer接收一个T行为,并且没有返回值,以下是其源码: ```java @FunctionalInterface public interface Consumer<T> { void accept(T t); } ``` 下面的例子展示了Consumer的用法: ```java import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class ConsumerTest { public static void main(String[] args) { List<Integer> numberList = new ArrayList<>(); Consumer<Integer> action = c -> System.out.println(c); filter(numberList, action); } static <T> void filter(List<T> list, Consumer<T> action) { for (T item : list) { action.accept(item); } } } ```