Part 3 – Method Reference in Java 8

Method Reference in Java 8

What have we learned so far

1. Functional Interface And Default Methods In Java 8 –
PART 1 : FUNCTIONAL INTERFACE AND DEFAULT METHODS IN JAVA 8

2. Lambda Expression in Java 8
PART 2 – LAMBDA EXPRESSION IN JAVA 8


What is Method Reference in Java 8?

Java provides new feature called Method reference to call the single method of functional interface.
Method reference is a short form of lambda expressions used on Functional Interface. We can replace our lambda expression with Method reference to clean the code.


Types of Method References in Java 8

1. Reference to Static Method

Suppose that we have a static method to print the value as printMe. Now we have a stream some integers and we want to print even numbers in forEach block, Here we have used Lambda expression and passed the current value to printMe method as below :

Without Method Reference :

package com.onlyfullstack.lambdas;

import java.util.stream.Stream;

public class MethodReference {

 private static void printMe(Integer input) {
  System.out.println("Printing "+ input);
 }
 
 private static void staticReference_WithoutMethodRef() {
  Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
  stream.filter(i -> i%2==0 )
   .forEach(i -> MethodReference.printMe(i));
 }
  
 public static void main(String[] args) {
  staticReference_WithoutMethodRef();  
 }
}

With Method Reference :
We can replace above code with Method Reference as below :

private static void staticReference_WithMethodRef() {
 Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
 stream.filter(i -> i%2==0 )
  .forEach(MethodReference::printMe);
}

2. Reference to Instance Method from Class type

We apply the Method Reference on the instance method with class name. We are using student.getName() to get the name of the student with Lambda expression. Map method will map the Student object to String object with getName() method.

Without Method Reference :

private static void instanceReferenceWithClass_WithoutMethodRef() {

 List<Student> students = JavaInputFixture.createList();
 
 Set<String> names = students.stream()
   .map(student -> student.getName()) // this will convert the Student Stream into String Stream by 
                   // applying the getName()
   .collect(Collectors.toSet());
 
 System.out.printf("Names of all students %sn", names);
}


With Method Reference :
We can change this expression and use Method Reference to remove the boilerplate code of lambda expression.

private static void instanceReferenceWithClass_WithMethodRef() {

 List<Student> students = JavaInputFixture.createList();
 
 Set<String> names = students.stream()
   .map(Student::getName) // this will convert the Student Stream into String Stream by 
           // applying the getName()
   .collect(Collectors.toSet());
 
 System.out.printf("Names of all students %sn", names);
}

Here
Student::getName is equivalent to
.map(student -> student.getName()) where student is an object of Student class

3. Reference to Instance Method from instance

We apply the Method Reference on the instance method with an instance. We are using System.out.println to print the numbers with Lambda expression.
Without Method Reference :

private static void instanceReference_WithoutMethodRef() {
 Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
 stream.filter(i -> i%2==0 )
  .forEach((i -> System.out.println(i)));
}

With Method Reference :

private static void instanceReference_WithMethodRef() {
 Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
 stream.filter(i -> i%2==0 )
  .forEach(System.out::println);
}

Source Code
Download source code of Java 8 features from below git repository :
java-8-features

Java 8 Features Tutorial

https://www.onlyfullstack.com/java-8-features-tutorial/

Lets go to our next tutorial where we will discuss below points :

Optional in Java 8
In this tutorial we will understand below topics – Why to use Optional in Java 8?
 – What is Optional in Java 8?
 – Different ways to create Optional?
 – What is the Difference in between Optional.of() and Optional.ofNullable()?
 – Optional.of() vs Optional.ofNullable() ?
 – Methods available in Optional
PART 4 – OPTIONAL IN JAVA 8