public static <T> Consumer<T> consumerWithIndex(BiConsumer<T, Integer> consumer) {
class Index {
int index;
}
Index index = new Index();
return t -> consumer.accept(t, index.index++);
}
public static void main(String[] args) {
List<Integer> list = List.of(1, 2, 4, 5, 6);
list.forEach(consumerWithIndex((item, index) -> {
System.out.println(index + " : " + item);
}));
AtomicInteger index = new AtomicInteger(0);
list.forEach(item -> {
System.out.println(index.incrementAndGet() + " " + item);
});
}
Java lambda 使用index
最新推荐文章于 2024-08-14 09:11:49 发布