lambda表达式——IntConsumer接口
代码实现:
package com.lambda;
import java.util.function.IntConsumer;
/*
* IntConsumer是一个功能接口
* public interface IntConsumer
* 表示接受单个int参数int返回任何结果的操作
* 这是一个functional interface,其功能方法是accept(int) 。
*
* */
public class IntConsumerDemo {
public static void repeat(int n, IntConsumer action){
for(int i=0;i<n;i++) action.accept(i);
}
public static void main(String[] args) {
repeat(10,i-> System.out.println("Countdown: "+(9-i)));
}
}
运行结果:
Countdown: 9
Countdown: 8
Countdown: 7
Countdown: 6
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Countdown: 0
Process finished with exit code 0

浙公网安备 33010602011771号