关注: Java提升营 ,最新文章第一时间送达, 10T 免费学习资料 随时领取!!!
双冒号(::)操作,也被称为方法引用运算符,用于直接调用指定类的方法。它的行为与lambda表达式完全相同。它与 lambda表达式 的唯一区别在于,它使用名称直接引用方法,而不是提供方法的委托。
语法:
<Class name>::<method name>
示例:打印 Stream
的所有元素:
stream.forEach(s-> System.out.println(s));
完整示例:
// Java code to print the elements of Stream // without using double colon operator import java.util.stream.*; class GFG { public static void main(String[] args) { // Get the stream Stream<String> stream = Stream.of("Geeks", "For", "Geeks", "A", "Computer", "Portal"); // Print the stream stream.forEach(s -> System.out.println(s)); } }
输出:
Geeks For Geeks A Computer Portal
stream.forEach(System.out::println(s));
完整示例:
// Java code to print the elements of Stream // using double colon operator import java.util.stream.*; class GFG { public static void main(String[] args) { // Get the stream Stream<String> stream = Stream.of("Geeks", "For", "Geeks", "A", "Computer", "Portal"); // Print the stream // using double colon operator stream.forEach(System.out::println); } }
输出:
Geeks For Geeks A Computer Portal
方法引用或 双冒号运算符 可用于以下方法:
如何在Java中使用方法引用:
语法:
(ClassName::methodName)
示例:
SomeClass::someStaticMethod
完整示例:
// Java code to show use of double colon operator // for static methods import java.util.*; class GFG { // static function to be called static void someFunction(String s) { System.out.println(s); } public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("For"); list.add("GEEKS"); // call the static method // using double colon operator list.forEach(GFG::someFunction); } }
输出:
Geeks For GEEKS
语法:
(objectOfClass::methodName)
示例:
System.out::println
完整示例:
// Java code to show use of double colon operator // for instance methods import java.util.*; class GFG { // instance function to be called void someFunction(String s) { System.out.println(s); } public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("For"); list.add("GEEKS"); // call the instance method // using double colon operator list.forEach((new GFG())::someFunction); } }
输出:
Geeks For GEEKS
语法:
(super :: methodName)
示例:
super::someSuperClassMethod
完整示例:
// Java code to show use of double colon operator // for super methods import java.util.*; import java.util.function.*; class Test { // super function to be called String print(String str) { return ("Hello " + str + "/n"); } } class GFG extends Test { // instance method to override super method @Override String print(String s) { // call the super method // using double colon operator Function<String, String> func = super::print; String newValue = func.apply(s); newValue += "Bye " + s + "/n"; System.out.println(newValue); return newValue; } // Driver code public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("For"); list.add("GEEKS"); // call the instance method // using double colon operator list.forEach(new GFG()::print); } }
输出:
Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS
语法:
(ClassName::methodName)
示例:
SomeClass::someInstanceMethod
完整示例:
// Java code to show use of double colon operator // for instance method of arbitrary type import java.util.*; class Test { String str=null; Test(String s) { this.str=s; } // instance function to be called void someFunction() { System.out.println(this.str); } } class GFG { public static void main(String[] args) { List<Test> list = new ArrayList<Test>(); list.add(new Test("Geeks")); list.add(new Test("For")); list.add(new Test("GEEKS")); // call the instance method // using double colon operator list.forEach(Test::someFunction); } }
输出:
Geeks For GEEKS
语法:
(ClassName::new)
示例:
ArrayList::new
完整示例:
// Java code to show use of double colon operator // for class constructor import java.util.*; class GFG { // Class constructor public GFG(String s) { System.out.println("Hello " + s); } // Driver code public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("For"); list.add("GEEKS"); // call the class constructor // using double colon operator list.forEach(GFG::new); } }
输出:
Hello Geeks Hello For Hello GEEKS