【JAVA8】双冒号
public class MyTest { public static void printValur(String str){ System.out.println("print value : "+str); } public static void main(String[] args) { List<String> al = Arrays.asList("a", "b", "c", "d"); //下面面的forEach循环和上面的循环是等价的
for (String a: al) { MyTest.printValur(a); } //下面的for each循环和上面的循环是等价的 al.forEach(x->{ MyTest.printValur(x); }); al.forEach(AcceptMethod::printValur); //下面的方法和上面等价的 Consumer<String> methodParam = MyTest::printValur; //方法参数 al.forEach(x -> methodParam.accept(x));//方法执行accept }}
上面的所有方法执行玩的结果都是如下:
print value : aprint value : bprint value : cprint value : d在JDK8中,接口Iterable 8中默认实现了forEach方法,调用了 JDK8中增加的接口Consumer内的accept方法,执行传入的方法参数。

浙公网安备 33010602011771号