Java 8新特性四:Double colon(::) operator

關注: Java提高營,最新文章第一時間送達, 10T 免費學習資料隨時領取!!!

雙冒號(::)操做,也被稱爲方法引用運算符,用於直接調用指定類的方法。它的行爲與la​​mbda表達式徹底相同。它與lambda表達式的惟一區別在於,它使用名稱直接引用方法,而不是提供方法的委託。java

語法:app

<Class name>::<method name>

示例:打印Stream的全部元素:ide

  • 使用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(s));

完整示例:this

// 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); 
    } 
}

輸出:code

Geeks
For
Geeks
A
Computer
Portal

何時和如何使用雙冒號運算符?

方法引用雙冒號運算符可用於如下方法:對象

  • 靜態方法
  • 實例方法
  • 構造函數

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

  1. 靜態方法

語法:element

(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
  1. 實例方法

語法:

(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
  1. 父類方法

語法:

(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
  1. 特定類型對象的實例方法

語法:

(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
  1. 類構造器

語法:

(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
相關文章
相關標籤/搜索