Java Method Reference Unit 3 Part 2
Java Method Reference Unit 3 Part 2
Java provides a new feature called method reference in Java 8. Method reference is used to
refer method of functional interface. It is compact and easy form of lambda expression. Each
time when you are using lambda expression to just referring a method, you can replace your
lambda expression with method reference. In this tutorial, we are explaining method
reference concept in detail.
Syntax
ContainingClass::staticMethodName
Example 1
In the following example, we have defined a functional interface and referring a static
method to it's functional method say().
interface Sayable{
void say();
}
public class MethodReference {
public static void saySomething(){
System.out.println("Hello, this is static method.");
}
public static void main(String[] args) {
// Referring static method
Sayable sayable = MethodReference::saySomething;
// Calling interface method
sayable.say();
}
}
Output:
Hello, this is static method.
Example 2
You can also override static methods by referring methods. In the following example, we
have defined and overloaded three add methods.
import java.util.function.BiFunction;
class Arithmetic{
public static int add(int a, int b){
return a+b;
}
public static float add(int a, float b){
return a+b;
}
public static float add(float a, float b){
return a+b;
}
}
public class MethodReference4 {
public static void main(String[] args)
{
BiFunction<Integer, Integer, Integer>adder1 = Arithmetic::add;
BiFunction<Integer, Float, Float>adder2 = Arithmetic::add;
BiFunction<Float, Float, Float>adder3 = Arithmetic::add;
int result1 = adder1.apply(10, 20);
float result2 = adder2.apply(10,
20.0f);
float result3 = adder3.apply(10.0f, 20.0f);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
Output:
30
30.0
30.0
Like static methods, you can refer instance methods also. In the following example, we are
describing the process of referring the instance method.
Syntax
containingObject::instanceMethodName
Example 1
In the following example, we are referring non-static methods. You can refer methods by
class object and anonymous object.
interface Sayable{
void say();
}
public class InstanceMethodReference {
public void saySomething(){
System.out.println("Hello, this is non-static method.");
}
public static void main(String[] args) {
InstanceMethodReference methodReference = new InstanceMethodReference(); //
Creating object
// Referring non-static method using reference
Sayable sayable = methodReference::saySomething;
// Calling interface method
sayable.say();
// Referring non-static method using anonymous object
Sayable sayable2 = new InstanceMethodReference()::saySomething; // You can use
anonymous object also
// Calling interface method
sayable2.say();
}
}
Output:
Hello, this is non-static method.
Hello, this is non-static method.
3) Reference to a Constructor
You can refer a constructor by using the new keyword. Here, we are referring constructor
with the help of functional interface.
Syntax
ClassName::new
Example
interface Messageable{
Message getMessage(String msg);
}
class Message{
Message(String
msg){
System.out.print(msg);
}
}
public class ConstructorReference {
public static void main(String[] args) {
Messageable hello = Message::new;
hello.getMessage("Hello");
}
}
Output
:
Hello
4.Stream In Java
Introduced in Java 8, Stream API is used to process collections of objects. A stream in Java
is a sequence of objects that supports various methods which can be pipelined to produce the
desired result.
Use of Stream in Java
There uses of Stream in Java are mentioned below:
Stream API is a way to express and process collections of objects.
Enable us to perform operations like filtering, mapping,reducing and sorting.
How to Create Java Stream?
Java Stream Creation is one of the most basic steps before considering the functionalities of
the Java Stream. Below is the syntax given on how to declare Java Stream.
Syntax
Stream<T> stream;
Here T is either a class, object, or data type depending upon the declaration.