Оператор подвійної двокрапки (::) у Java

The оператор подвійної двокрапки (::). , також відомий як оператор посилання на метод в Java використовується для виклику методу шляхом безпосереднього посилання на нього за допомогою його класу. Вони поводяться точно так само, як лямбда-вирази. Єдина відмінність, яку він має від лямбда-виразів, полягає в тому, що тут використовується пряме посилання на метод за назвою замість надання делегату методу.

Синтаксис:

 :: 

приклад: Щоб надрукувати всі елементи потоку:

  • Використання лямбда-виразу:
    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:

  1. Статичний метод

    Синтаксис:

    (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 
  2. Метод екземпляра

    Синтаксис:

    (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 
  3. Супер метод

    Синтаксис:

    (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 +> ' '> );> > }> }> > 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 +> ' '> ;> > 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 
  4. Метод екземпляра довільного об'єкта певного типу

    Синтаксис:

    (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 
  5. Конструктор класу

    Синтаксис:

    (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