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..
3.Reference 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 java.util.*; Predicate<Integer> p=new Predicate<Integer>()
{
import java.util.stream.*;
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 = Arrays.asList(3, 4, 6, 12, 20,25,7,90); }
}
Stream <Integer> s1= list.stream();
Stream<Integer> s2=s1.filter(num -> num % 5 == 0);
Stream<Integer> s3=s2.map(num -> num*2);
s3.forEach(num-> System.out.println(num));
}
}
Example of methods in Stream API
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Creating a list of Integers
List<Integer> list = Arrays.asList(3, 4, 6, 12, 20,25,7,90);
list.stream()
.filter(num -> num % 5 == 0)
.map(num-> num*2)
.forEach(num-> System.out.println(num));
}
}