Tuesday, August 10, 2021

Java 8: Functional Interfaces from Java.util.Function

In this article, I'm trying to provide a lambda implementation for all the default functional interfaces provided in Java8. Though these are sample ones, these should give an idea on how & when to use a particular functional interface. Java8 has added handful of default functional interface which are required for common processing. Ideally, one should/might not require to add a new functional interface other than this, let's check this out.

Below is our DB, which provides a list of employees.


       


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.defaultinterfaces;

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

import java8.Employee;
import java8.EmployeeDB;

public class DefaultFunctionalInterfaceDemo {

	public static void main(String[] args) {
		
		List<Employee> employees = EmployeeDB.getEmployeeList();
		
		//BiConsumer<T,U>Represents an operation that accepts two input arguments and returns no result.
		BiConsumer<String, Double> biConsumer = (a, b) -> System.out.println("Salary of Emp "+ a +" is " + b);
		biConsumer.accept(employees.get(0).getfName(), employees.get(0).getSalary());
		
		//BiFunction<T,U,R> Represents a function that accepts two arguments and produces a result.
		BiFunction<Integer,Double,String> biFunction = (a, b) -> {
									return "Salary of Emp "+employees.get(0).getfName() +"after dividing by "+ a +" is " + (b/a);
								};
		System.out.println(biFunction.apply(2,  employees.get(0).getSalary()));
		
		//BinaryOperator<T> Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
		BinaryOperator<String> binaryOperator = (a, b) -> a+" "+b;
		System.out.println("Full name of the employee is: "+ binaryOperator.apply(employees.get(0).getfName(), employees.get(0).getlName())) ;
		
		//BiPredicate<T,U>  Represents a predicate (boolean-valued function) of two arguments.
		BiPredicate<Double,Double> biPredicate = (a, b) -> (a>b);
		System.out.println("Salary of "+employees.get(0).getfName()+" is greater than salary of "
				+ ""+employees.get(1).getfName()+" :"+biPredicate.test(employees.get(0).getSalary(), employees.get(1).getSalary()));
		
		//BooleanSupplier Represents a supplier of boolean-valued results.
		BooleanSupplier booleanSupplier = () -> true;
		System.out.println("Value of booleanSupplier is "+ booleanSupplier.getAsBoolean());
		
		//Consumer<T>	 Represents an operation that accepts a single input argument and returns no result.
		Consumer<String> consumer = (a) -> System.out.println("Hi "+ a);
		consumer.accept(employees.get(0).getfName());
		
		//DoubleBinaryOperator Represents an operation upon two double-valued operands and producing a double-valued result.
		DoubleBinaryOperator dbo = (a, b) -> (a+b);
		System.out.println("Sum of salary of first 2 employees is :"+ dbo.applyAsDouble(employees.get(0).getSalary(), employees.get(1).getSalary()));
		
		//DoubleConsumer	 Represents an operation that accepts a single double-valued argument and returns no result.
		DoubleConsumer doubleConsumer = (a) -> System.out.println("Half of Sonu's salary is :"+ a/2);
		doubleConsumer.accept(employees.get(0).getSalary());
		
		//DoubleFunction<R> Represents a function that accepts a double-valued argument and produces a result.
		DoubleFunction<String> doubleFunction = (a) -> "Salary of Sonu is "+a;
		System.out.println(doubleFunction.apply(employees.get(0).getSalary()));
		
		//DoublePredicate	 Represents a predicate (boolean-valued function) of one double-valued argument.
		DoublePredicate doublePredicate = (a) -> a > 3000;
		System.out.println("Sonu's salary is greater than 3000: "+ doublePredicate.test(employees.get(0).getSalary()));
		
		//DoubleSupplier	 Represents a supplier of double-valued results.
		//Observe the difference between DoubleFunction<R> as well 
		DoubleSupplier doubleSupplier = () -> employees.get(0).getSalary();
		System.out.println("Salary of Sonu is :"+doubleSupplier.getAsDouble());
		
		//DoubleToIntFunction Represents a function that accepts a double-valued argument and produces an int-valued result.
		DoubleToIntFunction dtif = (d) -> (int) d;
		System.out.println("Integer value of "+employees.get(0).getSalary()/3+" is :"+dtif.applyAsInt(employees.get(0).getSalary()/3));
		
		//DoubleToLongFunction Represents a function that accepts a double-valued argument and produces a long-valued result.
		DoubleToLongFunction dtlf = (d) -> (long) d;
		System.out.println("Long value of "+employees.get(0).getSalary()/0.00003+" is :"+dtlf.applyAsLong(employees.get(0).getSalary()/0.00003));
		
		//DoubleUnaryOperator Represents an operation on a single double-valued operand that produces a double-valued result.
		DoubleUnaryOperator duo = (d) -> d*2;
		//using andThen function from DoubleUnaryOperator to add 500 in earlier operation 
		duo = duo.andThen(d-> d+500);
		System.out.println("Double of Sonu's salary + 500 is "+ duo.applyAsDouble(employees.get(0).getSalary()));
		
		//Function<T,R>	 Represents a function that accepts one argument and produces a result.
		Function<String,Integer> func = (a) -> a.length();
		func = func.andThen(a -> a*2);
		System.out.println("Two times Lenght of Sonu's full name is :"+func.apply(employees.get(0).getfName()+" "+employees.get(0).getlName()));
		
		//IntBinaryOperator Represents an operation upon two int-valued operands and producing an int-valued result.
		IntBinaryOperator ibo = (a, b) -> (a+b);
		System.out.println("Sum of employee IDs :"+ ibo.applyAsInt(employees.get(0).geteId(), employees.get(1).geteId()));
		
		//IntConsumer Represents an operation that accepts a single int-valued argument and returns no result.
		IntConsumer intConsumer = (a) -> System.out.println("Employee ID of Sonu is :"+a);
		intConsumer.accept(employees.get(0).geteId());
		
		//IntFunction<R> Represents a function that accepts an int-valued argument and produces a result.
		IntFunction<String> iFunc = (a) -> String.valueOf(a);
		System.out.println("String value of ID of Sonu is :"+iFunc.apply(employees.get(0).geteId()));
		
		//IntPredicate	Represents a predicate (boolean-valued function) of one int-valued argument.
		IntPredicate iPred = (a) -> a <= 5;
		System.out.println("Sonu's eID is less than equal to 5 :"+iPred.test(employees.get(0).geteId()));
		
		//IntSupplier Represents a supplier of int-valued results.
		IntSupplier iSupl = () -> employees.get(0).geteId();
		System.out.println("Sonu's eID is "+iSupl.getAsInt());
		
		//IntToDoubleFunction Represents a function that accepts an int-valued argument and produces a double-valued result.
		IntToDoubleFunction itdf = (a) -> (double) a; 
		System.out.println("Double value of Sonu's eID is :"+ itdf.applyAsDouble(employees.get(0).geteId()));
		
		//IntToLongFunction Represents a function that accepts an int-valued argument and produces a long-valued result.
		IntToLongFunction itlf = (a) -> (long) a; 
		System.out.println("Long value of Sonu's eID is :"+ itlf.applyAsLong(employees.get(0).geteId()));
		
		//IntUnaryOperator Represents an operation on a single int-valued operand that produces an int-valued result.
		IntUnaryOperator iuo = (a) -> 2*a;
		System.out.println("Two times of Sonu's eID is "+iuo.applyAsInt(employees.get(0).geteId()));
		
		//LongBinaryOperator Represents an operation upon two long-valued operands and producing a long-valued result.
		LongBinaryOperator lbo = (a, b) -> a+b;
		System.out.println("Sum of Long salary of first 2 employess is :"+ lbo.applyAsLong((long) employees.get(0).getSalary(), (long)employees.get(1).getSalary()));
		
		//LongConsumer Represents an operation that accepts a single long-valued argument and returns no result.
		LongConsumer lc = (a) -> System.out.println("Long Salary of Sonu is :"+ a);
		lc.accept((long) employees.get(0).getSalary());
		
		//LongFunction<R> Represents a function that accepts a long-valued argument and produces a result.
		LongFunction<String> lf = (a) -> String.valueOf(a);
		System.out.println("String value of Long salary of Sonu is :"+lf.apply((long) employees.get(0).getSalary()));
		
		//LongPredicate Represents a predicate (boolean-valued function) of one long-valued argument.
		LongPredicate lp = (a) -> a>700;
		System.out.println("Long salary of Sonu is greater than 700 :"+lp.test((long) employees.get(0).getSalary()));
		
		//LongSupplier Represents a supplier of long-valued results.
		LongSupplier ls = () -> (long) employees.get(0).getSalary();
		System.out.println("Long value of Sonu's salary is "+ls.getAsLong());
		
		//LongToDoubleFunction Represents a function that accepts a long-valued argument and produces a double-valued result.
		LongToDoubleFunction ltdf = (a) -> (double) a;
		System.out.println("Double value of long value of Sonu's salary is "+ (long) employees.get(0).getSalary());
		
		//LongToIntFunction Represents a function that accepts a long-valued argument and produces an int-valued result.
		LongToIntFunction ltif = (a) -> (int) a;
		System.out.println("Int value of long value of Sonu's salary is "+ (long) employees.get(0).getSalary());
		
		//LongUnaryOperator Represents an operation on a single long-valued operand that produces a long-valued result.
		LongUnaryOperator luo = (a) -> a*2;
		System.out.println("Two times Long salary of Sonu is :"+luo.applyAsLong((long) employees.get(0).getSalary()));
				
		//ObjDoubleConsumer<T> Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
		ObjDoubleConsumer<Employee> odc = (a, b) -> System.out.println("Two times of Sonu's salary is :"+ a.getSalary() * b);
		odc.accept(employees.get(0), 2);
		
		//ObjIntConsumer<T> Represents an operation that accepts an object-valued and a int-valued argument, and returns no result.
		ObjIntConsumer<Employee> oic = (a, b) -> System.out.println("Two times of Sonu's eID is :"+ a.geteId() * b);
		oic.accept(employees.get(0), 2);
		
		//ObjLongConsumer<T> Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
		ObjLongConsumer<Employee> olc = (a, b) -> System.out.println("Two times of Sonu's Long eID is :"+ (long)a.geteId() * b);
		olc.accept(employees.get(0), 2);
		
		//Predicate<T> Represents a predicate (boolean-valued function) of one argument.
		Predicate<Integer> predicate = a -> (a>5)?true:false;
		System.out.println("Number is greater than 5: "+ predicate.test(6));
		
		//Supplier<T> Represents a supplier of results.
		Supplier<Integer> sup = () -> 5;
		System.out.println("Supplied Integer value is :"+sup.get());
		
		//ToDoubleBiFunction<T,U> Represents a function that accepts two arguments and produces a double-valued result.
		ToDoubleBiFunction<Employee,Integer> tdbf = (a, b) -> a.getSalary()*b;
		System.out.println("5 times of Sonu's salary is "+tdbf.applyAsDouble(employees.get(0), 5));
		
		//ToDoubleFunction<T> Represents a function that produces a double-valued result.
		ToDoubleFunction<Employee> tdf = (a) -> a.getSalary()*3;
		System.out.println("3 times of Sonu's salary is "+tdf.applyAsDouble(employees.get(0)));
		
		//ToIntBiFunction<T,U> Represents a function that accepts two arguments and produces an int-valued result.
		ToIntBiFunction<Employee,Integer> tibf = (a, b) -> a.geteId()*b;
		System.out.println("5 times of Sonu's eID is "+tibf.applyAsInt(employees.get(0), 5));
		
		//ToIntFunction<T> Represents a function that produces an int-valued result.
		ToIntFunction<Employee> tif = (a) -> a.geteId()*3;
		System.out.println("3 times of Sonu's eID is "+tif.applyAsInt(employees.get(0)));
		
		//ToLongBiFunction<T,U> Represents a function that accepts two arguments and produces a long-valued result.
		ToLongBiFunction<Employee,Long> tlbf = (a, b) -> (long) a.getSalary()*b;
		System.out.println("5 times of Sonu's salary is "+tlbf.applyAsLong(employees.get(0), (long)5));
		
		//ToLongFunction<T> Represents a function that produces a long-valued result.
		ToLongFunction<Employee> tlf = (a) -> (long) a.geteId()*3;
		System.out.println("3 times of Sonu's eID is "+tlf.applyAsLong(employees.get(0)));
		
		//UnaryOperator<T> Represents an operation on a single operand that produces a result of the same type as its operand.
		UnaryOperator<Employee> uo = (a) -> a;
		System.out.println("Employee :"+uo.apply(employees.get(0)).toString());
		
	}

}

Add more practical examples, so that others can get help. Thanks.


No comments:

SpringBoot: Features: SpringApplication

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