How do I use the ObjLongConsumer functional interface in Java?

The ObjLongConsumer functional interface in Java is part of the java.util.function package and is available since Java 8. It represents an operation that accepts two input arguments: an object T and a long-valued argument, and performs some operation without returning any result. It is effectively a BiConsumer specialized for one object and one long argument.

Here’s a breakdown of how it works and how to use it:

Key Characteristics:

  1. Functional Interface:
    • It is annotated with @FunctionalInterface, meaning it contains a single abstract method to implement.
    • The method signature is:
void accept(T t, long value);
  1. Input:
    • A generic object T (the first parameter).
    • A long value (the second parameter).
  2. Output:
    • It does not return any value (similar to Consumer).
  3. Use Case:
    • Typically used in scenarios where we want to consume or process an object and a numeric value (e.g., processing an object with an associated count, index, or timestamp).

Example Usage

Here’s how we can use the ObjLongConsumer interface effectively:

package org.kodejava.util.function;

import java.util.function.ObjLongConsumer;

public class ObjLongConsumerExample {
   public static void main(String[] args) {
      // Create an ObjLongConsumer to print an object and a long value
      ObjLongConsumer<String> printDetails = (str, num) -> {
         System.out.println("String: " + str + ", Number: " + num);
      };

      // Use the ObjLongConsumer
      printDetails.accept("Example", 50);
   }
}

Output:

String: Example, Number: 50

Another Example with a Data Processor

Let’s see a more practical use case, for example, processing an object (like a product) with an associated long value (like its stock count):

package org.kodejava.util.function;

import java.util.function.ObjLongConsumer;

public class ObjLongConsumerExample2 {
   public static void main(String[] args) {
      ObjLongConsumer<Item> updateStock = (item, stock) -> {
         System.out.println("Item: " + item.name + ", Stock: " + stock);
      };

      Item apple = new Item("Apple");
      Item banana = new Item("Banana");

      // Update the stock of products
      updateStock.accept(apple, 100L);
      updateStock.accept(banana, 50L);
   }
}

class Item {
   String name;

   public Item(String name) {
      this.name = name;
   }
}

Output:

Item: Apple, Stock: 100
Item: Banana, Stock: 50

Real-World Use Cases

  1. Logging operations:
    • Log details of an event and a timestamp.
  2. Streams and Iteration:
    • It can be used in conjunction with streams where we need to process both an object and a primitive value like a long.

For example, using a loop with an index:

package org.kodejava.util.function;

import java.util.function.ObjLongConsumer;
import java.util.stream.IntStream;

public class ObjLongConsumerWithStreams {
   public static void main(String[] args) {
      ObjLongConsumer<String> indexedPrinter = (value, index) -> {
         System.out.println("Index: " + index + ", Value: " + value);
      };

      // Example: Using a range with an array
      String[] items = {"Apple", "Banana", "Cherry"};
      IntStream.range(0, items.length).forEach(i -> indexedPrinter.accept(items[i], i));
   }
}

Output:

Index: 0, Value: Apple
Index: 1, Value: Banana
Index: 2, Value: Cherry

Summary

  1. Use ObjLongConsumer when we need a functional interface that processes a combination of an object and a long value without returning a result.
  2. It is particularly useful for processing lists, objects with associated numeric properties, or items in streams with their indices.
  3. The accept method is where we specify what happens with the inputs.

This makes ObjLongConsumer a simple yet powerful tool for functional programming in Java!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.