Java 双冒号(::)运算符
双冒号(::)操作符,在Java中也被称为方法引用操作符,用于通过直接引用一个方法的类来调用它。它们的行为与lambda表达式完全一样。它与lambda表达式的唯一区别是,它使用直接引用方法的名称,而不是提供方法的委托。
语法。
<Class name>::<method name>
例子。要打印流中的所有元素。
- 使用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<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);
程序: 为了演示双冒号运算符的使用
// 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中使用方法引用。
- Static 方法
语法:
(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
- Instance 方法
语法:
(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 方法
语法:
(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
- Class 构造函数
语法:
(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