Wednesday, August 11, 2021

Java 8: Method reference & constructor reference

Method references are special types of lambda expressions. Like lambda expressions allow us to define an anonymous method and treat it as instance of functional interface; method referencing allows to do the same but using existing methods. Method referencing will be done using :: operator which is also known as Method Referencing Operator. There are four kinds of method references

1. Static methods

In this type, static methods in a class are referenced with :: operator

2. Instance methods of a particular object

Here we will create an instance of a class and call it's instance method

3. Instance methods of an arbitrary object of a particular type

It same as above, except that, in this, we will not be required to create an instance of the class

4. Constructor references

You can refer previously defined constructors and assign it to target type of a functional interface


Let's see all these types with below example


       


package java8;
import java.util.*;

public class EmployeeDB {
	public static List<Employee> getEmployeeList(){
		List<Employee> empList = Arrays.asList(
				//int eId, String fName, String lName, double salary, List<Integer> phNumbers
				new Employee(5, "Sonu", "Panjabi", 1000, Arrays.asList(1234, 2345)),
				new Employee(7, "Ganu", "Marathi", 1500, Arrays.asList(3456, 4567)),
				new Employee(8, "Manu", "Bangali", 2000, Arrays.asList(5678, 6789)),
				new Employee(4, "Tonu", "Bihari", 3000, Arrays.asList(7890, 8901)),
				new Employee(1, "Kanu", "Asami", 3500, Arrays.asList(9012, 1230)),
				new Employee(6, "Ranu", "Kashmiri", 4000, Arrays.asList(9876, 8765)),
				new Employee(2, "Janu", "Kannadi", 4500, Arrays.asList(7654, 6543)),
				new Employee(3, "Monu", "Gujrathi", 5000, Arrays.asList(5432, 4321))
				);
		return empList;
	}
}
package java8.methodreference;

import java.util.List;
import java.util.function.*;
import java.util.stream.Collectors;

import java8.*;

public class MethodReferenceDemo {

	public static void main(String[] args) {		
		//static method reference EmployeeDB::getEmployeeList
		Supplier<List<Employee>> supl = EmployeeDB::getEmployeeList;
		System.out.println("Total Employee size is: "+supl.get().size());
		
		//Instance methods of a particular object empComparator::compareEmployees
		EmpComparator empComparator = new EmpComparator();
		List<Employee> emps = supl.get().stream().sorted(empComparator::compareEmployees).collect(Collectors.toList());
		System.out.print("Here are the sorted eID: ");
		emps.stream().forEach(a -> System.out.print(a.geteId()+", "));
		System.out.println("");
		//OR Employee::getSalary
		Double totSalary = supl.get().stream().mapToDouble(Employee::getSalary).sum();
		System.out.println("Total Salary is: "+totSalary);
		
		//Instance methods of an arbitrary object of a particular type String::toUpperCase
		List<String> str = supl.get().stream().map(a -> a.getfName()).collect(Collectors.toList());
		System.out.println(str.stream().map(String::toUpperCase).collect(Collectors.toList()));
		
		//Constructor reference Employee::new
		Supplier<Employee> e = Employee::new;
		System.out.println("New employee is: "+e.get());
	}
}

class EmpComparator {
	public int compareEmployees(Employee e1, Employee e2) {
		return Integer.compare(e1.geteId(), e2.geteId());
	}
}

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...