🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 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); } } } ```