logo

Dubbel kolon (::) operator i Java

De dubbel kolon (::) operator , också känd som metodreferensoperatör i Java, används för att anropa en metod genom att referera till den med hjälp av dess klass direkt. De beter sig precis som lambda-uttrycken. Den enda skillnaden det har från lambda-uttryck är att detta använder direkt referens till metoden med namn istället för att ge en delegat till metoden.

Syntax:



 ::>

Exempel: Så här skriver du ut alla element i strömmen:

  • Använda Lambda-uttryck:
    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>
  • Använda dubbelkolonoperator:
    stream.forEach( System.out::println);>

    Program: För att demonstrera användningen av dubbelkolonoperator




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

    >

    java samlingar java
    >

    Produktion:

     Geeks For Geeks A Computer Portal>

När och hur använder man dubbelkolonoperator?

Metodreferens eller dubbelkolonoperator kan användas för att referera:

  • en statisk metod,
  • en instansmetod, eller
  • en konstruktör.

Så här använder du metodreferens i Java:

  1. Statisk metod

    Syntax:

    (ClassName::methodName)>

    Exempel:

    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. Instansmetod

    Syntax:

    (objectOfClass::methodName)>

    Exempel:

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

    >

    vad xd betyder

    >

    Produktion:

     Geeks For GEEKS>
  3. Supermetod

    Syntax:

    (super::methodName)>

    Exempel:

    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. Instansmetod för ett godtyckligt objekt av en viss typ

    Syntax:

    (ClassName::methodName)>

    Exempel:

    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. Klasskonstruktör

    Syntax:

    (ClassName::new)>

    Exempel:

    ArrayList::new>

    Program:

    mjukvarutestning och typer




    // 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>