The IntConsumer functional interface in Java belongs to the java.util.function package and is designed to represent an operation that accepts a single int argument and performs an operation without returning a result (i.e., it produces side effects typically).
1. Functional Interface Definition
@FunctionalInterface
public interface IntConsumer {
void accept(int value);
}
- Method:
accept(int value)- Takes a single
intargument. - Does not return anything, as it is focused on handling side effects.
- Takes a single
2. How to Use IntConsumer
We can implement it using lambdas, method references, or anonymous classes, and it is typically used for operations such as logging, printing, or updating state for a specific int value.
2.1 Using a Lambda Expression
Use a lambda expression to define the IntConsumer operation.
Example:
package org.kodejava.util.function;
import java.util.function.IntConsumer;
public class IntConsumerExample {
public static void main(String[] args) {
// Define IntConsumer to print a number with a message
IntConsumer printConsumer = value -> System.out.println("Received value: " + value);
// Use the consumer
// Output: Received value: 10
printConsumer.accept(10);
// Output: Received value: 42
printConsumer.accept(42);
}
}
2.2 Using Method References
We can use a predefined method (e.g., System.out::println) as an implementation.
Example:
package org.kodejava.util.function;
import java.util.function.IntConsumer;
public class IntConsumerMethodReferenceExample {
public static void main(String[] args) {
// Use System.out::println as an IntConsumer
IntConsumer printConsumer = System.out::println;
// Use the consumer to print numbers
// Output: 20
printConsumer.accept(20);
// Output: 55
printConsumer.accept(55);
}
}
3. Chaining IntConsumers
The IntConsumer interface has a default method called andThen that allows chaining multiple IntConsumer operations. Each consumer in the chain is executed in the order it is specified.
Example: Chaining Consumers
package org.kodejava.util.function;
import java.util.function.IntConsumer;
public class ChainingConsumers {
public static void main(String[] args) {
// First consumer: print the value
IntConsumer printConsumer =
value -> System.out.println("Printing: " + value);
// Second consumer: multiply the value by 2 and print
IntConsumer multiplyConsumer =
value -> System.out.println("Doubled value: " + (value * 2));
// Combine consumers with andThen
IntConsumer combinedConsumer = printConsumer.andThen(multiplyConsumer);
// Use the combined consumer
combinedConsumer.accept(5);
// Output:
// Printing: 5
// Doubled value: 10
}
}
4. Using IntConsumer in Streams
IntConsumer is commonly used in streams with operations such as forEach or peek to process elements.
Example: Using IntConsumer in a Stream
package org.kodejava.util.function;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class StreamIntConsumerExample {
public static void main(String[] args) {
// Define an IntConsumer for printing values
IntConsumer printConsumer = value -> System.out.println("Value: " + value);
// Use the IntConsumer with IntStream
IntStream.of(1, 2, 3, 4, 5)
.forEach(printConsumer);
// Output:
// Value: 1
// Value: 2
// Value: 3
// Value: 4
// Value: 5
}
}
Summary
IntConsumeris a functional interface for operations that take a singleintand cause side effects.- We can create
IntConsumerimplementations with lambdas, method references, or anonymous classes. - Use the
andThenmethod to chain multipleIntConsumerinstances. - Common use cases include stream operations (
forEach,peek) or any scenario where we need to handle anintvalue with side effects like printing, modifying state, etc.
