October 10, 2020

java.util.function.IntConsumer implementation using modern Java

In this post, I will show you different ways to implement java.util.function.IntConsumer functional interface.

You can watch me coding all of these implementation on youtube.

First one is a class implementation, where we use implements keyword to provide an implementation, we have been doing this since the start of Java.

class IntCons implements IntConsumer{
    
@Override
public void accept(final int intValue){
    System.out.print("Regular Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " ");
}
}

Next is , anonymous class implementation, this implementation looks like a variable declaration and a class definition combined together.

final IntConsumer anonIntCons = new IntConsumer(){
                                             @Override
                                             public void accept(final int intValue){
                                                 System.out.print("Anonymous Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " ");
                                             }
                                        };

Now, we will use Java’s modern feature Lambda .

final IntConsumer consLambda_1 = (int intValue) -> 
                              { System.out.print("consLambda_1 Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " "); };

The above implementation is a hybrid between a function definition, where we remove name of the function and use an arrow to point to the block that implements the function.

We can remove the data-type from above implementation, and compiler will figure out the data-type because it knows the method declaration of function defined in the interface IntConsumer. 

final IntConsumer consLambda_2 = (intValue) -> 
                              { System.out.print("consLambda_2 Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " "); };

We can use method reference to provide an implementation for the interface.

final IntConsumer consLambda_3 = consLambda_2::accept;

 

Entire Source Code: –

import java.util.function.IntConsumer;

import java.util.Arrays;

public class IntConsumerImpl {
    
    class IntCons implements IntConsumer{
        
    @Override
    public void accept(final int intValue){
        System.out.print("Regular Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " ");
    }
    }

    final IntConsumer anonIntCons = new IntConsumer(){
                                              @Override
                                              public void accept(final int intValue){
                                                  System.out.print("Anonymous Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " ");
                                              }
                                         };


    public static void main(final String... args){
        final IntConsumerImpl obj = new IntConsumerImpl();
    final IntCons cons = obj.new IntCons();

    final int[] ints = {1, 5, 10, 12};

    Arrays.stream(ints).peek(cons::accept).map(x -> x * x ).forEach(System.out::println);
    System.out.println();

    Arrays.stream(ints).peek(obj.anonIntCons::accept).map(x -> x * x ).forEach(System.out::println);
    System.out.println();

    final IntConsumer consLambda_1 = (int intValue) -> 
                              { System.out.print("consLambda_1 Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " "); };
    Arrays.stream(ints).peek(consLambda_1::accept).map(x -> x * x ).forEach(System.out::println);
    System.out.println();

    final IntConsumer consLambda_2 = (intValue) -> 
                              { System.out.print("consLambda_2 Class " + intValue + " The Value is less Than 10 " + (intValue < 10) + " "); };
    Arrays.stream(ints).peek(consLambda_2::accept).map(x -> x * x ).forEach(System.out::println);
    System.out.println();

    final IntConsumer consLambda_3 = consLambda_2::accept;
    Arrays.stream(ints).peek(consLambda_3::accept).map(x -> x * x ).forEach(System.out::println);
    System.out.println();

    }
}