Java 8 — Primitive Type Functional Interfaces | Code Factory

What is the Need of Primitive Type Functional Interfaces?

ArrayList<Integer> l = new ArrayList<Integer>(); // valid
ArrayList<int> l = new ArrayList<int>(); // invalid

Need of Primitive Functional Interfaces :

package com.codeFactory.primitiveFunctional;import java.util.function.Predicate;public class Test {
public static void main(String... args) {
int x[] = {0, 5, 10, 15, 20};
Predicate<Integer> p = i -> i%2 == 0;
for(int x1 : x) {
if(p.test(x1)) {
System.out.println(x1);
}
}
}
}
0
10
20

Primitive Type Functional Interfaces for Predicate :

@FunctionalInterface
public interface IntPredicate {
boolean test(int value);
default IntPredicate and(IntPredicate other) { }
default IntPredicate negate() { }
default IntPredicate or(IntPredicate other) { }
}
@FunctionalInterface
public interface LongPredicate {
boolean test(long value);
default LongPredicate and(LongPredicate other) { }
default LongPredicate negate() { }
default LongPredicate or(LongPredicate other) { }
}
@FunctionalInterface
public interface DoublePredicate {
boolean test(double value);
default DoublePredicate and(DoublePredicate other) { }
default DoublePredicate negate() { }
default DoublePredicate or(DoublePredicate other) { }
}
package com.codeFactory.primitiveFunctional;import java.util.function.IntPredicate;public class Test {
public static void main(String... args) {
int x[] = {0, 5, 10, 15, 20};
IntPredicate p = i -> i%2 == 0;
for(int x1 : x) {
if(p.test(x1)) {
System.out.println(x1);
}
}
}
}
0
10
20

Primitive Type Functional Interfaces for Function :

@FunctionalInterface
public interface IntFunction<R> {
R apply(int value);
}
@FunctionalInterface
public interface LongFunction<R> {
R apply(long value);
}
@FunctionalInterface
public interface DoubleFunction<R> {
R apply(double value);
}
@FunctionalInterface
public interface ToIntFunction<T> {
int applyAsInt(T value);
}
@FunctionalInterface
public interface ToLongFunction<T> {
long applyAsLong(T value);
}
@FunctionalInterface
public interface ToDoubleFunction<T> {
double applyAsDouble(T value);
}
@FunctionalInterface
public interface IntToLongFunction {
long applyAsLong(int value);
}
@FunctionalInterface
public interface IntToDoubleFunction {
double applyAsDouble(int value);
}
@FunctionalInterface
public interface LongToIntFunction {
int applyAsInt(long value);
}
@FunctionalInterface
public interface LongToDoubleFunction {
double applyAsDouble(long value);
}
@FunctionalInterface
public interface DoubleToIntFunction {
int applyAsInt(double value);
}
@FunctionalInterface
public interface DoubleToLongFunction {
long applyAsLong(double value);
}
@FunctionalInterface
public interface ToIntBiFunction<T, U> {
int applyAsInt(T t, U u);
}
@FunctionalInterface
public interface ToLongBiFunction<T,U> {
long applyAsLong(T t, U u);
}
@FunctionalInterface
public interface ToDoubleBiFunction<T,U> {
double applyAsDouble(T t, U u);
}
package com.codeFactory.primitiveFunctional;import java.util.function.Function;public class Test {
public static void main(String... args) {
Function<Integer, Integer> f = i -> i * i;
System.out.println(f.apply(5));
}
}
25
package com.codeFactory.primitiveFunctional;import java.util.function.IntFunction;public class Test {
public static void main(String... args) {
IntFunction<Integer> f = i -> i * i;
System.out.println(f.apply(5));
}
}
25
package com.codeFactory.primitiveFunctional;import java.util.function.Function;public class Test {
public static void main(String... args) {
Function<String, Integer> f = s -> s.length();
System.out.println(f.apply("Code Factory"));
}
}
12
package com.codeFactory.primitiveFunctional;import java.util.function.ToIntFunction;public class Test {
public static void main(String... args) {
ToIntFunction<String> f = s -> s.length();
System.out.println(f.applyAsInt("Code Factory"));
}
}
12
package com.codeFactory.primitiveFunctional;import java.util.function.Function;public class Test {
public static void main(String... args) {
Function<Integer, Double> f = i -> Math.sqrt(i);
System.out.println(f.apply(9));
}
}
3.0
package com.codeFactory.primitiveFunctional;import java.util.function.IntToDoubleFunction;public class Test {
public static void main(String... args) {
IntToDoubleFunction f = i -> Math.sqrt(i);
System.out.println(f.applyAsDouble(9));
}
}
3.0

Primitive Version for Consumer :

@FunctionalInterface
public interface IntConsumer {
void accept(int value);
default IntConsumer andThen(IntConsumer after) { }
}
@FunctionalInterface
public interface LongConsumer {
void accept(long value);
default LongConsumer andThen(LongConsumer after) { }
}
@FunctionalInterface
public interface DoubleConsumer {
void accept(double value);
default DoubleConsumer andThen(DoubleConsumer after) { }
}
@FunctionalInterface
public interface ObjIntConsumer<T> {
void accept(T t, int value);
}
@FunctionalInterface
public interface ObjLongConsumer<T> {
void accept(T t, long value);
}
@FunctionalInterface
public interface ObjDoubleConsumer<T> {
void accept(T t, double value)
}

Demo Program for IntConsumer :

package com.codeFactory.primitiveFunctional;import java.util.function.IntConsumer;public class Test {
public static void main(String... args) {
IntConsumer c = i -> System.out.println("Square : " + i*i);
c.accept(5);
}
}
Square : 25
package com.codeFactory.primitiveFunctional;import java.util.ArrayList;
import java.util.function.ObjDoubleConsumer;
class Employee {
String name;
double salary;

public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + "]";
}
}
public class Test {
public static void main(String... args) {
ArrayList<Employee> i = new ArrayList<>();
populate(i);

System.out.println("Before Increment :");
print(i);

ObjDoubleConsumer<Employee> c = (e, d) -> e.salary += d;
for(Employee e : i) {
c.accept(e, 750);
}

System.out.println("After Increment :");
print(i);
}

private static void populate(ArrayList<Employee> i) {
i.add(new Employee("Narendra", 1000));
i.add(new Employee("Amit", 2000));
i.add(new Employee("Vijay", 3000));
i.add(new Employee("Yogi", 4000));
}

private static void print(ArrayList<Employee> i) {
for(Employee e : i) {
System.out.println(e);
}
}
}
Before Increment :
Employee [name=Narendra, salary=1000.0]
Employee [name=Amit, salary=2000.0]
Employee [name=Vijay, salary=3000.0]
Employee [name=Yogi, salary=4000.0]
After Increment :
Employee [name=Narendra, salary=1750.0]
Employee [name=Amit, salary=2750.0]
Employee [name=Vijay, salary=3750.0]
Employee [name=Yogi, salary=4750.0]

Primitive Versions for Supplier :

@FunctionalInterface
public interface IntSupplier {
int getAsInt();
}
@FunctionalInterface
public interface LongSupplier {
long getAsLong();
}
@FunctionalInterface
public interface DoubleSupplier {
double getAsDouble();
}
@FunctionalInterface
public interface BooleanSupplier {
boolean getAsBoolean();
}
package com.codeFactory.primitiveFunctional;import java.util.function.IntSupplier;public class Test {
public static void main(String... args) {
IntSupplier s = () -> (int)(Math.random() * 10);

System.out.println("OTP : " + generateOTP(s));
System.out.println("OTP : " + generateOTP(s));
System.out.println("OTP : " + generateOTP(s));
}

private static String generateOTP(IntSupplier s) {
String otp = "";
for(int i=0; i<6; i++) {
otp += s.getAsInt();
}
return otp;
}
}
OTP : 429493
OTP : 579145
OTP : 888907

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store