java8中的Consumer方法

public class ConsumerTest {

    public void test(Consumer consumer){
        consumer.accept(100);
    }

    public static void main(String[] args) {
        ConsumerTest consumerTest = new ConsumerTest();

        Consumer consumer = i->System.out.println(i);
        IntConsumer intConsumer = i-> System.out.println(i);

        consumerTest.test(consumer);//面向对象式的对象传递
        consumerTest.test(intConsumer::accept);//函数时方式,传递行为--等价于下面
        consumerTest.test(i -> System.out.println(i));//

    }
}

你可能感兴趣的:(java8中的Consumer方法)