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 in Java 8 with Examples...!!! Click To Tweet
Look at ObjIntConsumer Javadoc description below:
ObjIntConsumer Interface contains following method:
accept
Let’s discuss this method:
accept
This method performs operation on the given arguments and return no result.
1 | void accept(T t, int value); |
Below are the several examples to demonstrate accept() method:
Example 1. with Integer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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; } } |
ObjIntConsumer Interface in Java 8 with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8