Home core java Java 8 Consumer with examples

Java 8 Consumer with examples

1. What is Java 8 Consumer?

Java 8 Consumer is a built-in functional interface that represents an operation that accepts a single input argument and returns no result. This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

In contrast Java 8 provides Supplier functional interface as well, that takes no argument and returns a result.

2. Consumer interface methods :

Modifier and TypeMethod and Description
voidaccept(T t)– Performs this operation on the given argument. T denotes the type of the input argument to the operation
default Consumer<T>andThen(Consumer<? super T> after) – Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.

3. Consumer accept() method Example

Following example shows you how to use accept() method of Consumer interface.

public class ConsumerDemo1 {
    
     public static void main(String[] args) {
          
           Consumer<String> consumer = s -> System.out.println(s);
           
           Consumer<String> consumerString = s -> System.out.println(s.toUpperCase());
           
           Consumer<Integer> intConsumer = i -> System.out.println(5*i);
           
           // accept() - Performs this operation on the given argument.
           /********************************************************/
           consumer.accept("Peter Milanovich");// Peter Milanovich
           consumerString.accept("High priority");// HIGH PRIORITY
           intConsumer.accept(4);//20
     }
}

Output :

Peter Milanovich
HIGH PRIORITY
20

4. Java 8 Consumer andThen() method example

Following example shows you how to use andThen() method of Consumer interface.

public class ConsumerDemo2 {
    
    public static void main(String[] args) {
         
          Consumer<Integer> c1 = i -> System.out.println(5*i);
          Consumer<Integer> c2 = i -> System.out.println(5+i);
          
          /* andThen(Consumer<? super T> after) - Returns a composed Consumer that performs,
          in sequence, this operation followed by the after operation.*/
          /********************************************************/
         c1.andThen(c2).accept(4); // 20, 9
         System.out.println("");
         c2.andThen(c1).accept(5); //10, 25
         System.out.println("");
         c1.andThen(i -> System.out.println(3*i)).accept(4); // 20, 12
    }
}

Output :

20
9

10
25

20
12

5. Primitive specializations of the Java 8  Consumer interface

Following are the primitive Specializations for Consumer interface in java.util.function package.

  • IntConsumer  – Represents a Consumer operation accepts single int-valued argument and returns no results. Having one method void accept(int value).
  • LongConsumer – Represents a Consumer operation accepts single  long-valued argument and returns no results. Having one method void accept(long value).
  • DoubleConsumer – Represents a Consumer operation accepts single  double-valued argument and returns no results. Having one method  void accept(double value).
  • BooleanConsumer  – Represents a Consumer operation accepts single  boolean-valued argument and returns no results. Having one method  void accept(boolean value).

5.1. Example

Following example is to demonstrate Primitive specializations of the Java 8  Consumer interface.

public class ConsumerPrimitiveSpecializationsDemo {
    
    public static void main(String[] args) {
         
          Consumer<Integer> c1 = i -> System.out.println(5*i);
          Consumer<Long> c2 = i -> System.out.println(i);
          Consumer<Double> c3 = i -> System.out.println(i+20.0);
          Consumer<Boolean> c4 = i -> System.out.println(i);
          
          c1.accept(10);
          c2.accept(new Date().getTime());
          c3.accept(50.20);
          c4.accept(10%2==0);      
    }
}

Output :

50
1581265213888
70.2
true

Conclusion

in this article we covered Consumer interface in Java 8. In conclusion Consumer is a functional interface which represents single value and return nothing.

References

  1. Java documentation
  2. java.util.function package tree

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories