Java8新特性四:Double colon(::) operator | Java提升营

Java8新特性四:Double colon(::) operator

双冒号(::)操作,也被称为方法引用运算符,用于直接调用指定类的方法。它的行为与la​​mbda表达式完全相同。它与lambda表达式的唯一区别在于,它使用名称直接引用方法,而不是提供方法的委托。

语法:

1
<Class name>::<method name>

示例:打印Stream的所有元素:

  • 使用Lambda表达式:
1
stream.forEach(s-> System.out.println(s));

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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));
}
}

输出:

1
2
3
4
5
6
Geeks
For
Geeks
A
Computer
Portal
  • 使用双冒号运算符:
1
stream.forEach(System.out::println(s));

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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);
}
}

输出:

1
2
3
4
5
6
Geeks
For
Geeks
A
Computer
Portal

什么时候和如何使用双冒号运算符?

方法引用双冒号运算符可用于以下方法:

  • 静态方法
  • 实例方法
  • 构造函数

如何在Java中使用方法引用:

  1. 静态方法

语法:

1
(ClassName::methodName)

示例:

1
SomeClass::someStaticMethod

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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);
}
}

输出:

1
2
3
Geeks
For
GEEKS
  1. 实例方法

语法:

1
(objectOfClass::methodName)

示例:

1
System.out::println

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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);
}
}

输出:

1
2
3
Geeks
For
GEEKS
  1. 父类方法

语法:

1
(super :: methodName)

示例:

1
super::someSuperClassMethod

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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);
}
}

输出:

1
2
3
4
5
6
7
8
Hello Geeks
Bye Geeks

Hello For
Bye For

Hello GEEKS
Bye GEEKS
  1. 特定类型对象的实例方法

语法:

1
(ClassName::methodName)

示例:

1
SomeClass::someInstanceMethod

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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);
}
}

输出:

1
2
3
Geeks
For
GEEKS
  1. 类构造器

语法:

1
(ClassName::new)

示例:

1
ArrayList::new

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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);
}
}

输出:

1
2
3
Hello Geeks
Hello For
Hello GEEKS
给老奴加个鸡腿吧 🍨.