In this article, we will discuss different variation of pre-defined Consumer Functional Interface available for primitive data-types like int, long, double, etc. in Java 1.8 version
1. Primitive Consumer Functional Interface (1-input argument) :
- This is very similar to Consumer Functional Interface where it accepts 1 input argument of primitive-type like int, long, double, etc. only whereas Consumer Functional Interface allows to accept any data-type
- Both Consumer Functional Interface and Primitive Consumer Functional Interface doesn’t return result
- Performance-wise primitive Consumer Functional Interface is much faster compared to Consumer<T>
- There are lot of conversion happening for auto-boxing & auto-unboxing for converting primitive-type to wrapper-type and again wrapper-type to primitive-type and so on
- To avoid unnecessary conversion between primitive-type to wrapper-type and vice-versa, primitive-type specific Consumer Functional Interface for conversion introduced in Java 1.8 version as listed below,
- IntConsumer
- LongConsumer
- DoubleConsumer
We will look into method signature along with example for each one of the above primitive Consumer Functional Interface
1.1 IntConsumer Functional Interface
- This primitive IntConsumer Functional Interface accepts 1-input argument of int primitive-type and it is not required to declare while defining IntConsumer (or lambda expression)
- Method signature: void accept(int value);
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface IntConsumer {
    /**
     * Performs this operation on the given argument.
     *
     * @param value the input argument
     */
    void accept(int value);
    // other default and static methods
}
1.1.1 Example for IntConsumer :
- Here, while defining lambda expression using IntConsumer we haven’t specified any data-type like Integer for input argument, still compiler doesn’t complain and executed well
- Caution: but if we pass any other data-type like String or double then compile-time error will be thrown stating “The method accept(int) in the type IntConsumer is not applicable for the arguments (double)“
package net.bench.resources.primitive.consumer.example;
import java.util.function.IntConsumer;
public class IntConsumerExample {
	public static void main(String[] args) {
		// lambda expression to print square using IntConsumer FI
		IntConsumer ic = i -> System.out.println("Square of " + i + " is = " + (i*i));
		// test with different numbers
		ic.accept(5);
		ic.accept(9);
		ic.accept(17);
		ic.accept(23);
		ic.accept(31);
	}
}
Output:
Square of 5 is = 25
Square of 9 is = 81
Square of 17 is = 289
Square of 23 is = 529
Square of 31 is = 961
1.2 LongConsumer Functional Interface
- This primitive LongConsumer accepts 1-input argument of long primitive-type and it is not required to declare while defining LongConsumer (or lambda expression)
- Method signature: void accept(long value);
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface LongConsumer {
    /**
     * Performs this operation on the given argument.
     *
     * @param value the input argument
     */
    void accept(long value);
   // other default and static methods
}
1.2.1 Example for LongConsumer :
- Here, while defining lambda expression using LongConsumer we haven’t specified any data-type like Long for input argument, still compiler doesn’t complain and executed well
- Caution: but if we pass any other data-type like String or double then compile-time error will be thrown stating “The method accept(long) in the type LongConsumer is not applicable for the arguments (double)“
package net.bench.resources.primitive.consumer.example;
import java.util.function.LongConsumer;
public class LongConsumerExample {
	public static void main(String[] args) {
		// lambda expression to print square using LongConsumer FI
		LongConsumer lc = longNum -> {
			long cube = longNum*longNum*longNum;
			System.out.println("Cube of " + longNum + " is = " + cube);
		};
		// test with different numbers
		lc.accept(147);
		lc.accept(258);
		lc.accept(963);
		lc.accept(1256);
		lc.accept(9845);
	}
}
Output:
Cube of 147 is = 3176523
Cube of 258 is = 17173512
Cube of 963 is = 893056347
Cube of 1256 is = 1981385216
Cube of 9845 is = 954217026125
1.3 DoubleConsumer Functional Interface
- This primitive DoubleConsumer accepts 1-input argument of double primitive-type and it is not required to declare while defining DoubleConsumer (or lambda expression)
- Method signature: void accept(double value);
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface DoubleConsumer {
    /**
     * Performs this operation on the given argument.
     *
     * @param value the input argument
     */
    void accept(double value);
   // other default and static methods
}
1.3.1 Example for DoubleConsumer :
- Here, while defining lambda expression using DoubleConsumer we haven’t specified any data-type like Double for input argument, still compiler doesn’t complain and executed well
- Caution: but if we pass any other data-type like String then compile-time error will be thrown stating “The method accept(double) in the type DoubleConsumer is not applicable for the arguments (String)“
package net.bench.resources.primitive.consumer.example;
import java.util.function.DoubleConsumer;
public class DoubleConsumerExample {
	public static void main(String[] args) {
		// lambda expression to print square using DoubleConsumer FI
		DoubleConsumer dc = d -> System.out.println("Square of " + d + " is \t= " + (d*d));
		// test with different numbers
		dc.accept(5.2);
		dc.accept(9.14);
		dc.accept(17.258);
		dc.accept(22.7);
		dc.accept(3.14152);
	}
}
Output:
Square of 5.2 is 	= 27.040000000000003
Square of 9.14 is 	= 83.53960000000001
Square of 17.258 is 	= 297.83856399999996
Square of 22.7 is 	= 515.29
Square of 3.14152 is 	= 9.869147910399999
2. Primitive Consumer Functional Interface (2 input arguments) :
- This is very similar to BiConsumer Functional Interface where it accepts 2 input argument of Object-type and primitive-type like int, long, double, etc, whereas BiConsumer allows to accepts any data-type and doesn’t return result
- Performance-wise primitive Consumer Functional Interface is much faster compared to Consumer<T, U>
- There are lot of conversion happening for auto-boxing & auto-unboxing for converting primitive-type to wrapper-type and again wrapper-type to primitive-type and so on
- To avoid unnecessary conversion between primitive-type to wrapper-type and vice-versa, primitive-type specific Consumer Functional Interface for conversion introduced in Java 1.8 version as listed below,
- ObjIntConsumer
- ObjLongConsumer
- ObjDoubleConsumer
We will look into method signature along with example for each one of the above primitive Consumer Functional Interface
2.1 ObjIntConsumer Functional Interface
- This primitive ObjIntConsumer Functional Interface accepts 2 input arguments
- Where 1st argument is Object-type
- 2nd argument is primitive-type int only
- It is not required to declare while defining ObjIntConsumer (or lambda expression)
- Method signature: void accept(T t, int value);
package java.util.function;
import java.util.function.BiConsumer;
@FunctionalInterface
public interface ObjIntConsumer<T> {
    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param value the second input argument
     */
    void accept(T t, int value);
}
2.1.1 Example for ObjIntConsumer :
- Here, while defining lambda expression using ObjIntConsumer
- We are specifying Object-type for 1st argument only i.e.; String
- Not specifying any type for 2nd argument as Integer, still compiler doesn’t complain and executed well
- Below example depicts multiplication after conversion of String to Integer
package net.bench.resources.primitive.consumer.example;
import java.util.function.ObjIntConsumer;
public class ObjIntConsumerExample {
	public static void main(String[] args) {
		// lambda expression for multiply using ObjIntConsumer FI
		ObjIntConsumer<String> oic = (str, i) -> {
			int multiply = Integer.parseInt(str) * i;
			System.out.println(str + " * " + i + " \t= " + multiply);
		};
		// testing for above lambda
		oic.accept("2", 100);
		oic.accept("31", 200);
		oic.accept("587", 300);
		oic.accept("2431", 400);
		oic.accept("98563", 500);
	}
}
Output:
2 * 100 	= 200
31 * 200 	= 6200
587 * 300 	= 176100
2431 * 400 	= 972400
98563 * 500 	= 49281500
2.2 ObjLongConsumer Functional Interface
- This primitive ObjLongConsumer Functional Interface accepts 2 input argument
- Where 1st argument is Object-type
- 2nd argument is primitive-type long only
- It is not required to declare while defining ObjLongConsumer (or lambda expression)
- Method signature: void accept(T t, long value);
package java.util.function;
@FunctionalInterface
public interface ObjLongConsumer<T> {
    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param value the second input argument
     */
    void accept(T t, long value);
}
2.2.1 Example for ObjLongConsumer :
- Here, while defining lambda expression using ObjLongConsumer
- We are specifying Object-type for 1st argument only i.e.; String
- Not specifying any type for 2nd argument as Long, still compiler doesn’t complain and executed well
- Below example prints console output displaying arguments passed
package net.bench.resources.primitive.consumer.example;
import java.util.function.ObjLongConsumer;
public class ObjLongConsumerExample {
	public static void main(String[] args) {
		// lambda expression for ObjLongConsumer FI
		ObjLongConsumer<String> olc = 
				(str, longNum) -> System.out.println(str + " ranked at " + longNum);
				// testing for above lambda
				olc.accept("BenchResources.Net", 554037);
				olc.accept("Oracle.com", 11414);
				olc.accept("Microsoft.com", 96584);
				olc.accept("Apple.com", 23654);
				olc.accept("Google.com", 85476);
	}
}
Output:
BenchResources.Net ranked at 554037
Oracle.com ranked at 11414
Microsoft.com ranked at 96584
Apple.com ranked at 23654
Google.com ranked at 85476
2.3 ObjDoubleConsumer Functional Interface
- This primitive ObjDoubleConsumer Functional Interface accepts 2 input arguments
- Where 1st argument is Object-type
- 2nd argument is primitive-type double only
- It is not required to declare while defining ObjDoubleConsumer (or lambda expression)
- Method signature: void accept(T t, double value);
package java.util.function;
@FunctionalInterface
public interface ObjDoubleConsumer<T> {
    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param value the second input argument
     */
    void accept(T t, double value);
}
2.3.1 Example for ObjDoubleConsumer :
- Here, while defining lambda expression using ObjDoubleConsumer
- We are specifying Object-type for 1st argument only i.e.; String
- Not specifying any type for 2nd argument as Double, still compiler doesn’t complain and executed well
- Below example prints console output displaying arguments passed
package net.bench.resources.primitive.consumer.example;
import java.util.function.ObjDoubleConsumer;
public class ObjDoubleConsumerExample {
	public static void main(String[] args) {
		// lambda expression for ObjDoubleConsumer FI
		ObjDoubleConsumer<String> odc = (str, d) -> {
			System.out.println(str + " offers " + d + " % discounts");
		};
		// testing for above lambda
		odc.accept("FBB", 10.25);
		odc.accept("Amazon", 22.125);
		odc.accept("Flipkart", 15.32);
		odc.accept("Myntra", 0.975);
		odc.accept("Snapdeal", 5.25);
	}
}
Output:
FBB offers 10.25 % discounts
Amazon offers 22.125 % discounts
Flipkart offers 15.32 % discounts
Myntra offers 0.975 % discounts
Snapdeal offers 5.25 % discounts
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/function/IntConsumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/LongConsumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/DoubleConsumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/ObjIntConsumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/ObjLongConsumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/ObjDoubleConsumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Happy Coding !!
Happy Learning !!