Java8::⽤法(JDK8双冒号⽤法)
jdk8中使⽤了::的⽤法。就是把⽅法当做参数传到stream内部,使stream的每个元素都传⼊到该⽅法⾥⾯执⾏⼀下,双冒号运算就是Java中的[⽅法引⽤],[⽅法引⽤]的格式是
类名::⽅法名java stream
注意此处没有()。
案例:
表达式:
person -> Age();
使⽤双冒号:
Person::getAge
表达式:
new HashMap<>()
使⽤双冒号:
HsahMap :: new
部分代码案例
未使⽤双冒号
public class MyTest {
public static void main(String[] args) {
List<String> a1 = Arrays.asList("a", "b", "c");
for (String a : a1) {
printValur(a);
};
a1.forEach(x -> MyTest.printValur(x));
}
public static void printValur(String str) {
System.out.println("print value : " + str);
}
}
使⽤后
a1.forEach(MyTest::printValur);
Consumer<String> consumer = MyTest::printValur;
a1.forEach(x -> consumer.accept(x));
未使⽤双冒号:
List<String> list = a1.stream().map(x -> x.toUpperCase()).List());
System.out.String());
使⽤双冒号:
ArrayList<String> collect = a1.stream().map(String::toUpperCase).Collection(ArrayList::new));
System.out.String());

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。