ObjIntConsumer 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 an object-valued and a int-valued argument as input but produces no output. In this post, we are going to see several implementations of ObjIntConsumer Interface by using different examples.

ObjIntConsumer_Interface_Java8_Techndeck

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

Look at ObjIntConsumer Javadoc description below:

ObjIntConsumerInterface_Signature_Java8_Techndeck

ObjIntConsumer 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, int value);

Below are the several examples to demonstrate accept() method:

Example 1. with Integer

import java.util.function.ObjIntConsumer;

public class ObjIntConsumerInterfaceJava8Example1 {

    public static void main(String args[]) {

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

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

        objIntConsumberObj.accept(5, 10);

    }
}

Example 2. with String

import java.util.function.ObjIntConsumer;

public class ObjIntConsumerInterfaceJava8Example2 {

    public static void main(String args[]) {

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

        ObjIntConsumer <String> objIntConsumberObj = (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.");
            }
        };

        objIntConsumberObj.accept("justanordinarywriter.com", 15);

        objIntConsumberObj.accept("techndeck.com", 15);
    }
}

Example 3. with ArrayList

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

public class ObjIntConsumerInterfaceJava8Example3 {

    public static void main(String args[]) {

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

        ObjIntConsumer <List <Integer>> objIntConsumberObj = (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 <Integer> listObj = new ArrayList <Integer> ();
        listObj.add(25);
        listObj.add(100);
        listObj.add(73);

        objIntConsumberObj.accept(listObj, 75);

        objIntConsumberObj.accept(listObj, 100);
    }
}

Example 4. with Custom Class Object

import java.util.function.ObjIntConsumer;

public class ObjIntConsumerInterfaceJava8Example4 {

    public static void main(String args[]) {

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

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

        Persons p1 = new Persons(25, "Isha Durani");
        Persons p2 = new Persons(10, "Gaurangee");

        objIntConsumberObj.accept(p1, 25);

        objIntConsumberObj.accept(p2, 100);
    }
}


class Persons {
    int id;
    String name;

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

    Persons(int id, String name) {
        this.id = id;
        this.name = name;
    }

}

Java 8 ObjIntConsumer 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.

ObjIntConsumer 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