The dvojno dvopičje (::) operator , poznan tudi kot referenčni operator metode v Javi se uporablja za klic metode tako, da se nanjo sklicuje neposredno s pomočjo njenega razreda. Obnašajo se natanko tako kot lambda izrazi. Edina razlika, ki jo ima od lambda izrazov, je ta, da uporablja neposredno sklicevanje na metodo po imenu, namesto da bi metodi zagotovila pooblaščenca.
Sintaksa:
::>
primer: Če želite natisniti vse elemente toka:
- Uporaba lambda izraza:
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));>>}>}>>
>
Izhod:Geeks For Geeks A Computer Portal>
- Uporaba operatorja z dvojnim dvopičjem:
stream.forEach( System.out::println);>
Program: Za prikaz uporabe operatorja dvojnega dvopičja
// 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);>>}>}>>
normalizacija v bazi podatkov
>
Izhod:niz predmetov v Javi
Geeks For Geeks A Computer Portal>
Kdaj in kako uporabiti operator dvojnega dvopičja?
Za sklicevanje na metodo ali operator dvojnega dvopičja lahko uporabite:
- statična metoda,
- metoda primerka, oz
- konstruktor.
Kako uporabiti referenco metode v Javi:
- Statična metoda
Sintaksa:
(ClassName::methodName)>
primer:
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);>>}>}>>
>
Izhod:Geeks For GEEKS>
- Metoda primerka
Sintaksa:
(objectOfClass::methodName)>
primer:
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);>>}>}>>
>
Izhod:Geeks For GEEKS>
- Super metoda
Sintaksa:
(super::methodName)>
primer:
super::someSuperClassMethod>
Program:
ubežni znak 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);>>}>}>>
>
Izhod:Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
- Metoda primerka poljubnega predmeta določene vrste
Sintaksa:
(ClassName::methodName)>
primer:
SomeClass::someInstanceMethod>
Program:
ekta kapoor igralec
// 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);>>}>}>>
>
Izhod:Geeks For GEEKS>
- Konstruktor razreda
Sintaksa:
(ClassName::new)>
primer:
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>);>>}>}>>
>
Izhod:Hello Geeks Hello For Hello GEEKS>