java8::(JDK8双冒号)具有什么功能呢?
下文笔者讲述JDK8中有双冒号的功能说明,如下所示
jdk8中双冒号的功能:
将方法作为参数传入至stream内部
stream内部中每个元素都会放入到方法中运行
例:未采用双冒号的写法
public class TestClass {
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");
for (String a: al) {
TestClass.printValur(a);
}
al.forEach(x->{
TestClass.printValur(x);
});
}
}
采用JDK双冒号的写法
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");
al.forEach(TestClass::printValur);
//下面的方法和上面等价的
Consumer<String> methodParam = TestClass::printValur; //方法参数
al.forEach(x -> methodParam.accept(x));//方法执行accept
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


