The operátor dvojitej dvojbodky (::). , taktiež známy ako operátor referencie metódy v jazyku Java sa používa na volanie metódy tak, že sa na ňu priamo odkazuje pomocou svojej triedy. Správajú sa presne ako lambda výrazy. Jediný rozdiel, ktorý má od výrazov lambda, je ten, že sa používa priamy odkaz na metódu podľa názvu namiesto poskytnutia delegáta metóde.
Syntax:
::>
Príklad: Ak chcete vytlačiť všetky prvky streamu:
- Použitie výrazu lambda:
stream.forEach( s->System.out.println(s));>
Program:
// 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));>>}>}>ak inak bash
>
>
Výkon:Geeks For Geeks A Computer Portal>
- Použitie operátora dvojitej dvojbodky:
stream.forEach( System.out::println);>
Program: Na demonštráciu použitia operátora s dvojitou dvojbodkou
// 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);>>}>}>>
>
Výkon:Geeks For Geeks A Computer Portal>
Kedy a ako používať operátor dvojitej dvojbodky?
Odkaz na metódu alebo operátor dvojitej dvojbodky možno použiť na odkazovanie:
ssis tutoriál
- statická metóda,
- metóda inštancie, príp
- konštruktér.
Ako používať odkaz na metódu v jazyku Java:
- Statická metóda
Syntax:
(ClassName::methodName)>
Príklad:
SomeClass::someStaticMethod>
Program:
// 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);>>}>}>>
>
Výkon:Geeks For GEEKS>
- Metóda inštancie
Syntax:
(objectOfClass::methodName)>
Príklad:
System.out::println>
Program:
// 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);>>}>}>>
>
čo je desktop ini
Výkon:Geeks For GEEKS>
- Super metóda
Syntax:
(super::methodName)>
Príklad:
super::someSuperClassMethod>
Program:
reťazec do poľa java
// 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);>>}>}>>
>
Výkon:Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
- Metóda inštancie ľubovoľného objektu určitého typu
Syntax:
(ClassName::methodName)>
Príklad:
SomeClass::someInstanceMethod>
Program:
// 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);>>}>}>>
>
Výkon:Geeks For GEEKS>
- Konštruktor triedy
Syntax:
(ClassName::new)>
Príklad:
ArrayList::new>
Program:
// 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>);>>}>}>>
>
herec rohit Shetty
Výkon:Hello Geeks Hello For Hello GEEKS>