Java Method References
Java Method References
1. Reference to a static method
2. Reference to a instance method
(Example without using reference)
(Example with using reference to an instance
method)
Cont..
[Link] to a Constructor
Note:
When functional Interface return object type, then we can use
Constructor reference.
Syntax:
ClassName::new
Example 1:
Example: 2
Example of methods in Stream API
import [Link].*; Predicate<Integer> p=new Predicate<Integer>()
{
import [Link].*;
public Boolean test(Integer num)
class GFG { {
public static void main(String[] args) if( n%5==0)
{ return true;
else
// Creating a list of Integers return false;
List<Integer> list = [Link](3, 4, 6, 12, 20,25,7,90); }
}
Stream <Integer> s1= [Link]();
Stream<Integer> s2=[Link](num -> num % 5 == 0);
Stream<Integer> s3=[Link](num -> num*2);
[Link](num-> [Link](num));
}
}
Example of methods in Stream API
import [Link].*;
import [Link].*;
class GFG {
public static void main(String[] args)
{
// Creating a list of Integers
List<Integer> list = [Link](3, 4, 6, 12, 20,25,7,90);
[Link]()
.filter(num -> num % 5 == 0)
.map(num-> num*2)
.forEach(num-> [Link](num));
}
}