0% found this document useful (0 votes)
3 views2 pages

Jaba Interview

The document discusses the Consumer functional interface in Java, highlighting its use for introducing side effects in a modular way, such as logging, updating counters, and sending notifications. It also describes other functional interfaces like Predicate, Function, and Supplier, detailing their purposes and examples of use. Each interface serves specific roles in processing data, performing actions, transforming inputs, or generating results.

Uploaded by

Yanet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Jaba Interview

The document discusses the Consumer functional interface in Java, highlighting its use for introducing side effects in a modular way, such as logging, updating counters, and sending notifications. It also describes other functional interfaces like Predicate, Function, and Supplier, detailing their purposes and examples of use. Each interface serves specific roles in processing data, performing actions, transforming inputs, or generating results.

Uploaded by

Yanet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CONSUMER The Consumer functional interface is perfect for introducing such side

effects in a clean and modular way.

1. Loggin as a side effect

public int processData(int input, Consumer<String> logger) {


logger.accept("Processing data: " + input); // Log side effect
return input * 2; // Main function result remains unchanged
}
Consumer<String> logToConsole = msg -> System.out.println(msg);
int result = processData(5, logToConsole); // Side effect: logs the message, but return value
is still the computed result

2. Updating counter
You can use a counter as a side effect if you need to track the number of times a function is
called or how many elements have been processed. This can be useful for metrics, analytics, or
just debugging.

AtomicInteger counter = new AtomicInteger(0);

Consumer<Integer> countConsumer = value -> counter.incrementAndGet(); // Side effect:


increment the counter
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(countConsumer); // Processes each number and increments the counter

System.out.println("Number of elements processed: " + counter.get()); // Output: 5

3. Sending Notifications
When a certain condition is met, you might want to trigger a notification (like an email, SMS, or
push notification) as a side effect without altering the primary function’s result.

public int processOrder(Order order, Consumer<Order> notificationSender) {


if (order.isHighPriority()) {
notificationSender.accept(order); // Side effect: send notification
}
return order.process(); // Main function: process the order}

Consumer<Order> sendEmailNotification = order -> {


// Code to send an email
System.out.println("Sending notification for order: " + order.getId());
};

Order myOrder = new Order(123, true); // high priority order


int orderResult = processOrder(myOrder, sendEmailNotification); // Sends email as a side
effect if high priority

In this example, the main task is processing the order, but if the order is marked as high priority,
a notification (side effect) is sent to the relevant parties.
1. Predicate<T>:
• Takes: A single input (T).
• Does: Evaluates a condition or test on the input (T).
• Returns: A boolean (true or false) result.
• Use Case: When you need to perform a conditional check on data (like filtering or
validation).
• Example:
Predicate<Integer> isEven = n -> n % 2 == 0;
boolean result = isEven.test(4); // Returns true (4 is even)

2. Consumer<T>:
• Takes: A single input (T).
• Does: Performs an action on the input (side effects).
• Returns: No result (void).
• Use Case: When you want to process the data but don’t need to return a
result.
• Example:
Consumer<String> print = s -> System.out.println(s);
print.accept("Hello"); // Prints "Hello"

3. Function<T, R>:
• Takes: A single input (T).
• Does: Transforms the input into a result (R).
• Returns: A result (R).
• Use Case: When you want to transform or map an input to a result.
• Example:
Function<String, Integer> lengthFunction = s -> s.length();
int length = lengthFunction.apply("Hello"); // Returns 5

4. Supplier<R>:
• Takes: No input.
• Does: Produces a result (R), often on demand.
• Returns: A result (R).
• Use Case: When you need to supply or generate values without any input,
such as generating random numbers or getting the current time.
• Example:

Supplier<Double> randomSupplier = () -> Math.random();


double randomValue = randomSupplier.get(); // Returns a random double

• Predicate<T>: Tests a condition on the input (T) and returns a boolean.


• Consumer<T>: Takes an input and performs an action without returning a result.
• Function<T, R>: Transforms an input into a result (R).
• Supplier<R>: Provides a result without needing any input.

You might also like