の 二重コロン (::) 演算子 、 としても知られている メソッド参照演算子 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 でメソッド参照を使用する方法:
- 静的メソッド
構文:
(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>
- インスタンスメソッド
構文:
(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>
- スーパーメソッド
構文:
(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>
- 特定の型の任意のオブジェクトのインスタンス メソッド
構文:
(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);>>}>}>Javaは文字列を整数に変換します
>
>
出力:Geeks For GEEKS>
- クラスコンストラクター
構文:
(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>