The Wayback Machine - https://web.archive.org/web/20240204231524/https://www.geeksforgeeks.org/java-8-objdoubleconsumer-interface-with-example/
Open In App
Related Articles

Java 8 | ObjDoubleConsumer Interface with Example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The ObjDoubleConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result. However these kind of functions don’t return any value.

Hence this functional interface takes in one generic namely:-

  • T: denotes the type of the input argument to the operation

The lambda expression assigned to an object of ObjDoubleConsumer type is used to define its accept() which eventually applies the given operation on its argument. It takes in a double-valued and a T-valued argument and is expected to operate without any side effects. It is more like using an object of type BiConsumer<T, Double> except the fact that one is a reference and one is a primitive data type in this case. Hence we will be obtaining one reference and one value when this function is called.

Functions in ObjDoubleConsumer Interface

The ObjDoubleConsumer interface consists of the following two functions:

1. accept()

This method accepts two values and performs the operation on the given arguments.

Syntax:

void accept(T t, double value)

Parameters: This method takes in two parameters:

  • t– the first input argument
  • value– the second input argument

Return Value: This method does not return any value.

Below is the code to illustrate accept() method:

Program:




// Java code to demonstrate
// accept() method of ObjDoubleConsumer Interface
  
import java.util.Arrays;
import java.util.List;
import java.util.function.ObjDoubleConsumer;
import java.util.stream.Stream;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Get the list from which
        // the Interface is to be instantiated.
        List<Integer>
            arr = Arrays.asList(3, 2, 5, 7, 4);
  
        // Instantiate the ObjDoubleConsumer interface
        ObjDoubleConsumer<List<Integer> >
            func = (list, num) ->
        {
            list.stream()
                .forEach(
                    a -> System.out.println(a * num));
        };
        func.accept(arr, 2.0);
    }
}


Output:

6.0
4.0
10.0
14.0
8.0

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 26 Oct, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials