jdk8新特性之forEach循环遍历

List numbers = new ArrayList<>();

//no.1
numbers.forEach((Integer integer) -> {
    System.out.println(integer);
});

//no.2
numbers.forEach(integer -> {
    System.out.println(integer);
});

//no.3
numbers.forEach(integer -> System.out.println(integer));

//no.4
numbers.forEach(System.out::println);

//no.5
numbers.forEach(new MyConsumer());//基于MyConsumer类



MyConsumer类如下,主要是接口的实现

class MyConsumer implements Consumer{
    @Override
    public void accept(Integer integer) {
        System.out.println(integer);
    }
}

你可能感兴趣的:(jdk8新特性之forEach循环遍历)