Java’s functional Consumer interface

Quite often, a Java Stream or other component needs an object passed thereto to perform some form of calculation or process, but when the method is complete, nothing returns from the tactic. this is often where Java’s functional Consumer interface comes in handy. According to JavaDoc, the patron interface accepts any object style as input. The java.util.function.

The consumer class has one non-default method named accept which takes one object as its argument and contains a void return type.

eg.    java.util.function.Consumer<T>

Functional programming with Consumer

Sometimes programmers of new lambdas and streams get intimidated by the components defined within the java.util.function package, so I always wish to remind developers that each one of all the interfaces defined during this package follows all of the standards and pre-Java 8 rules for implementing interfaces.

As such, you’ll incorporate the functional Consumer interface into your code just by creating a category that implements the java.util.function.Consumer, or by coding an inner class

 Implement a Consumer with a Java class

Here is the Java Consumer function implemented using a Java class instead of a lambda expressions

class ConsumerExample implements Consumer<Long> {

  public void accept(Long t) {

            System.out.println(t*t);

  }

}

Inside a main method or any other piece of Java code, the ConsumerExample class can be instantiated according to traditional Java syntax rules

/* Java Consumer example using a class */

ConsumerExample ce = new ConsumerExample();

ce.accept(new Long(4));

 

Also, an inner class can be used

/* Functional Consumer example using inner class */

Consumer<Long> innerConsumer = new Consumer<Long>() {




  @Override

  public void accept(Long u) {

            System.out.println(u*u);

  }

};

I like to use a verbose lambda syntax when demonstrating how they work, but one in all the explanations for using lambda expressions is to form Java less verbose. in order that the lambda expression above is written in a way more concise manner this is often the feature of consumer interfaces

Consumer<Long> conciseLambda = U -> System.out.println(U*U);

conciseLambda.accept(new Long(30))

 

Sample Consumer interface use cases-

The functional Consumer interface is employed extensively across the Java API, with a variety of classes within the java.util.function package, like ObjIntConsumer, BIConsumer, and IntConsumer providing extended support to the fundamental interface.

Furthermore, a spread of methods within the Java Stream API takes the functional Consumer interface as an argument, including methods like collect, forEach, and peek.

There are only some key interfaces you would like to master so as to become a competent functional programmer. If you understand the concepts laid and move into this functional Consumer interface example, you’re well on your thanks to mastering the updated Java APIs.

Consumer tutorial code

 

            import java.util.ArrayList; 

            import java.util.List; 

            import java.util.function.Consumer; 

            public class ConsumerInterfaceExample { 

            static void addList(List<Integer> list){   

            int result = list.stream() 

                        .mapToInt(Integer::intValue) 

                        .sum(); 

            System.out.println("Sum of list values: "+result);

            } 

            public static void main(String[] args) {  

                        List<Integer> list = new ArrayList<Integer>(); 

                        list.add(10); 

                        list.add(20); 

                        list.add(30); 

                        list.add(40);   

            Consumer<List<Integer>> consumer = ConsumerInterfaceExample::addList; 

            consumer.accept(list);

             

            } 

            }

Leave a Reply