Java中的双冒号 (::)运算符
双冒号 (::)运算符,在Java中也称为方法引用运算符,用于在类的帮助下直接引用方法来调用方法。它们的行为与 lambda 表达式完全相同。它与 lambda 表达式的唯一区别是它使用名称直接引用方法,而不是为方法提供委托。
句法:
::
示例:打印流的所有元素:
- 使用 Lambda 表达式:
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
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);
程序:演示双冒号运算符的使用
// 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
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
list = new ArrayList (); 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
list = new ArrayList (); 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
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 list = new ArrayList (); 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
list = new ArrayList (); 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
list = new ArrayList (); 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