ObjDoubleConsumer Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects a single an object-valued and a double-valued argument as an input and produces no output. In this post, we are going to see several implementations of ObjDoubleConsumer Interface by using different examples.

ObjDoubleConsumer_Interface_Java8_Techndeck

ObjDoubleConsumer Interface in Java 8 with Examples...!!! Share on X

Look at ObjDoubleConsumer Javadoc description below:

ObjDoubleConsumerInterface_Signature_Java8_Techndeck

ObjDoubleConsumer Interface contains following method:

accept

Let’s discuss this method:

accept 

This method performs operation on the given arguments and return no result.

void accept(T t, double value);

Below is an example to demonstrate accept() method:

Example 1. with Integer

import java.util.function.ObjDoubleConsumer;

public class ObjDoubleConsumerInterfaceJava8Example1 {

    public static void main(String args[]) {

        System.out.println("Ex. 1 - ObjDoubleConsumer - Integer example \n");

        ObjDoubleConsumer <Integer> objIntConsumberObj = (t, value) -> System.out.println(t * value);

        objIntConsumberObj.accept(5, 10);

    }
}

Example 2. with String

import java.util.function.ObjDoubleConsumer;

public class ObjDoubleConsumerInterfaceJava8Example2 {

    public static void main(String args[]) {

        System.out.println("Ex. 2 - ObjDoubleConsumer - String example \n");

        ObjDoubleConsumer <String> objDoubleConsumberObj = (t, value) -> {
            if (t.length() > value) {
                System.out.println("String is bigger than the expected value.");
            } else if (t.length() == value) {
                System.out.println("String lenght is equal to expected value.");
            } else {
                System.out.println("String is shorter than the expected value.");
            }
        };

        objDoubleConsumberObj.accept("justanordinarywriter.com", 15.0);

        objDoubleConsumberObj.accept("techndeck.com", 15.0);
    }
}

Example 3. with ArrayList

import java.util.ArrayList;
import java.util.List;
import java.util.function.ObjDoubleConsumer;

public class ObjDoubleConsumerInterfaceJava8Example3 {

    public static void main(String args[]) {

        System.out.println("Ex. 3 - ObjDoubleConsumer - list example \n");

        ObjDoubleConsumer < List < Double >> objDoubleConsumberObj = (t, value) - > {
            if (t.contains(value)) {
                System.out.println("Expected value is present in the string");
            } else {
                System.out.println("Expected value is not present in the string");
            }
        };

        List < Double > listObj = new ArrayList < Double > ();
        listObj.add(25.0);
        listObj.add(100.4577);
        listObj.add(73.77777777777);

        objDoubleConsumberObj.accept(listObj, 100.4577);

        objDoubleConsumberObj.accept(listObj, 200);
    }
}

Example 4. with Custom Class Object

package org.personal.samples;

import java.util.function.ObjDoubleConsumer;

public class ObjDoubleConsumerInterfaceJava8Example4 {

    public static void main(String args[]) {

        System.out.println("Ex. 4 - ObjDoubleConsumer - Custom class obj example \n");

        ObjDoubleConsumer < Agent > ObjDoubleConsumberObj = (t, value) - > {
            if (t.getId() == value) {
                System.out.println("\nPerson with Id: " + t.getId() + " exist." + "\nHis/Her name is: " + t.getName());
            } else {
                System.out.println("\nPerson with Id: " + t.getId() + " does not exist.");
            }
        };

        Agent p1 = new Agent(25.5555, "Isha Durani");
        Agent p2 = new Agent(10.1000, "Gaurangee");

        ObjDoubleConsumberObj.accept(p1, 5475.0055);

        ObjDoubleConsumberObj.accept(p2, 10.1000);
    }
}


class Agent {
    double id;
    String name;

    public double getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    Agent(double id, String name) {
        this.id = id;
        this.name = name;
    }

}

Java 8 ObjDoubleConsumer Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

ObjDoubleConsumer Interface in Java 8 with Examples...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts