Dobbelt kolon (::) operator i Java

Det dobbelt kolon (::) operator , også kendt som metode reference operatør i Java, bruges til at kalde en metode ved at henvise til den ved hjælp af dens klasse direkte. De opfører sig præcis som lambda-udtrykkene. Den eneste forskel, det har fra lambda-udtryk, er, at dette bruger direkte reference til metoden ved navn i stedet for at give en delegeret til metoden.

Syntaks:

 :: 

Eksempel: Sådan udskrives alle elementer i strømmen:

  • Brug af Lambda-udtryk:
    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));> > }> }>

    Produktion:

     Geeks For Geeks A Computer Portal 
  • Brug af dobbelt kolon operator:
    stream.forEach( System.out::println); 

    Program: For at demonstrere brugen af ​​dobbelt kolon operator




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

    Produktion:

     Geeks For Geeks A Computer Portal 

Hvornår og hvordan bruger man dobbelt kolon operator?

Metodereference eller dobbeltkolonoperator kan bruges til at referere:

  • en statisk metode,
  • en instansmetode, eller
  • en konstruktør.

Sådan bruges metodereference i Java:

  1. Statisk metode

    Syntaks:

    (ClassName::methodName) 

    Eksempel:

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

    Produktion:

     Geeks For GEEKS 
  2. Forekomst metode

    Syntaks:

    (objectOfClass::methodName) 

    Eksempel:

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

    Produktion:

     Geeks For GEEKS 
  3. Super metode

    Syntaks:

    (super::methodName) 

    Eksempel:

    super::someSuperClassMethod 

    Program:




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

    Produktion:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS 
  4. Forekomstmetode af et vilkårligt objekt af en bestemt type

    Syntaks:

    (ClassName::methodName) 

    Eksempel:

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

    Produktion:

     Geeks For GEEKS 
  5. Klassekonstruktør

    Syntaks:

    (ClassName::new) 

    Eksempel:

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

    Produktion:

     Hello Geeks Hello For Hello GEEKS