0% found this document useful (0 votes)
6 views13 pages

Java Refernces

The document discusses Java method references, including references to static methods, instance methods, and constructors. It provides examples of using method references in the context of the Stream API to filter and manipulate a list of integers. The syntax for constructor references is also mentioned, highlighting their use when a functional interface returns an object type.

Uploaded by

SHILPI BANSAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Java Refernces

The document discusses Java method references, including references to static methods, instance methods, and constructors. It provides examples of using method references in the context of the Stream API to filter and manipulate a list of integers. The syntax for constructor references is also mentioned, highlighting their use when a functional interface returns an object type.

Uploaded by

SHILPI BANSAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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

}
}

You might also like