How to implement ObjLongConsumer interface using lambda expression in Java?


ObjLongConsumer<T> is a functional interface from java.util.function package. This interface accepts an object-valued and long-valued argument as input but doesn't produce any output. ObjLongConsumer<T> can be used as an assignment target for lambda expression and method reference and contains only one abstract method: accept().

Syntax

@FunctionalInterface
public interface ObjLongConsumer<T> {
 void accept(T t, long value)
}

Example

import java.util.function.ObjLongConsumer;

public class ObjLongConsumerTest {
   public static void main(String[] args) {
      ObjLongConsumer<Employee> olc = (employee, number) -> {     // lambda expression
         if(employee != null) {
            System.out.println("Employee Name: " + employee.getEmpName());
            System.out.println("Old Mobile No: " + employee.getMobileNumber());
            employee.setMobileNumber(number);
            System.out.println("New Mobile No: " + employee.getMobileNumber());
         }
      };
      Employee empObject = new Employee("Adithya", 123456789L);
      olc.accept(empObject, 987654321L);
   }
}

// Employee class
class Employee {
   private String empName;
   private Long mobileNum;
   public Employee(String empName, Long mobileNum){
      this.empName = empName;
      this.mobileNum = mobileNum;
   }
   public String getEmpName() {
      return empName;
   }
   public void setEmpName(String empName) {
      this.empName = empName;
   }
   public Long getMobileNumber() {
      return mobileNum;
   }
   public void setMobileNumber(Long mobileNum) {
      this.mobileNum = mobileNum;
   }
}

Output

Employee Name: Adithya
Old Mobile No: 123456789
New Mobile No: 987654321

Updated on: 14-Jul-2020

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements