Learnitweb

Primitive type functional interfaces

Before we start, we should spend a minute to see what is autoboxing and unboxing.

Autoboxing is the automatic conversion that the Java compiler does between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer.

Integer i = 10;

Converting an object of a wrapper type to its corresponding primitive value is called unboxing. For example, converting Integer to int.

Integer i = new Integer(10);
int primitive = i;

Need of primitive functional interfaces

In the case of normal Functional interfaces (like Predicate, Function, Consumer etc) input and return types are always Object types. If we pass primitive values then these primitive values will be converted to Object type (autoboxing). This creates performance problems as conversion steps are required.

Let us understand this with the help of an example.

import java.util.function.Predicate;

public class Test {

	public static void main(String args[]) {
		int numbers[] = { 0, 1, 2, 3, 4, 5 };
		Predicate<Integer> p = i -> i % 2 == 0;
		for (int number : numbers) {
			if (p.test(number)) {
				System.out.println(number);
			}
		}
	}
}

In this code, we are using an array of int. The Predicate ‘p’ is of generic type Integer. So, for all int values in ‘numbers’ autoboxing will be done. This is a performance problem. To overcome this problem primitive functional interfaces are there for help. Primitive functional interface can take primitive types as input and return primitive types. Hence autoboxing and autounboxing won’t be required, which improves performance.

Primitive Type Functional Interfaces for Predicate

IntPredicateRepresents a predicate which accepts one int-valued argument.
LongPredicateRepresents a predicate which accepts one long-valued argument.
DoublePredicateRepresents a predicate which accepts one double-valued argument.
import java.util.function.IntPredicate;

public class IntPredicateExample {
	public static void main(String... args) {
		int numbers[] = { 1, 2, 3, 4, 5, 6 };
		IntPredicate p = i -> i % 2 == 0;
		for (int number : numbers) {
			if (p.test(number)) {
				System.out.println(number);
			}
		}
	}
}

Primitive Type Functional Interfaces for Function

IntFunction<R>Represents a function that accepts an int-valued argument and produces a result.
LongFunction<R>Represents a function that accepts a long-valued argument and produces a result.
DoubleFunction<R>Represents a function that accepts a double-valued argument and produces a result.
ToIntFunction<T>Represents a function that produces an int-valued result.
ToLongFunction<T>Represents a function that produces a long-valued result.
ToDoubleFunction<T>Represents a function that produces a double-valued result.
IntToLongFunctionRepresents a function that accepts an int-valued argument and produces a long-valued result.
IntToDoubleFunctionRepresents a function that accepts an int-valued argument and produces a double-valued result.
LongToIntFunctionRepresents a function that accepts a long-valued argument and produces an int-valued result.
LongToDoubleFunctionRepresents a function that accepts a long-valued argument and produces a double-valued result.
DoubleToIntFunctionRepresents a function that accepts a double-valued argument and produces an int-valued result.
DoubleToLongFunctionRepresents a function that accepts a double-valued argument and produces a long-valued result.
ToIntBiFunction<T,U>Represents a function that accepts two arguments and produces an int-valued result.
ToLongBiFunction<T,U>Represents a function that accepts two arguments and produces a long-valued result.
ToDoubleBiFunction<T,U>Represents a function that accepts two arguments and produces a double-valued result.

Program to find square of given integer by using IntFunction

import java.util.function.IntFunction;

public class IntFunctionExample {
	public static void main(String... args) {
		IntFunction<Integer> f = i -> i * i;
		System.out.println(f.apply(10));
	}
}

Primitive Type Functional Interface for Consumer

IntConsumerRepresents an operation that accepts a single int-valued argument and returns no result.
LongConsumerRepresents an operation that accepts a single long-valued argument and returns no result.
DoubleConsumerRepresents an operation that accepts a single double-valued argument and returns no result.
import java.util.function.IntConsumer;

public class IntConsumerExample {
	public static void main(String... args) {
		IntConsumer c = i -> System.out.println("Square : " + i * i);
		c.accept(10);
	}
}

Primitive Type Functional Interface for Supplier

IntSupplierRepresents a supplier of int-valued results.
LongSupplierRepresents a supplier of long-valued results.
DoubleSupplierRepresents a supplier of double-valued results.
BooleanSupplierRepresents a supplier of boolean-valued results.

Program to generate 6 digit otp using IntSupplier

import java.util.function.IntSupplier;

public class BiFunctionExample {
	
	public static void main(String[] args) {
		String otp = "";
		IntSupplier s = () -> (int)(Math.random() * 10);
		for(int i=0; i<6; i++) {
			otp = otp + s.getAsInt();
		}
		
		System.out.println(otp);
	}
}