
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement DoubleConsumer Using Lambda Expression in Java
DoubleConsumer is a functional interface from java.util.function package. This functional interface accepts a single double-valued argument as input and produces no output. This interface can be used as an assignment target for a lambda expression or method reference. DoubleConsumer contains one abstract method: accept() and one default method: andThen().
Syntax
@FunctionalInterface public interface DoubleConsumer { void accept(double value); }
Example-1
import java.util.function.DoubleConsumer; public class DoubleConsumerLambdaTest1 { public static void main(String args[]) { DoubleConsumer increment = doubleVal -> { // lambda expression System.out.println("Incrementing " + doubleVal + " by one"); System.out.println("Current Value : "+ (doubleVal+1)); }; DoubleConsumer decrement = doubleVal -> { // lambda expression System.out.println("Decrementing " + doubleVal + " by one"); System.out.println("Current Value : " + (doubleVal-1)); }; DoubleConsumer result = increment.andThen(decrement); result.accept(777); } }
Output
Incrementing 777.0 by one Current Value : 778.0 Decrementing 777.0 by one Current Value : 776.0
Example-2
import java.util.Arrays; import java.util.function.DoubleConsumer; public class DoubleConsumerLambdaTest2 { public static void main(String[] args) { double[] numbers = {4.7d, 7d, 8.2d, 6.8d, 10.5d, 3.2d}; DoubleConsumer dconsumer = d -> System.out.print(d + " "); // lambda expression Arrays.stream(numbers).forEach(dconsumer); } }
Output
4.7 7.0 8.2 6.8 10.5 3.2
Advertisements