GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Java 8 | Consumer Interface in Java with Examples - GeeksforGeeks Java 8 | Consumer Interface in Java with ExamplesLast Updated : 1 Oct, 2026 The Consumer Interface is a functional interface available in the java.util.function package, introduced in Java 8. It represents an operation that accepts a single input argument and performs an action on it without returning any result. Consumer is commonly used for operations such as printing values, modifying collections, logging data, and performing side effects.
Output Hello, John Explanation: In this example, a Consumer<String> accepts a string value and prints a greeting message. The accept() method passes "John" to the Consumer, which performs the operation without returning any value. Syntax
T: denotes the type of the input argument to the operation. Methods of Consumer InterfaceThe Consumer interface consists of the following two functions: 1. accept() MethodThe accept() method accepts a single argument and performs the specified operation on it. Since Consumer is designed for side effects, this method does not return any value. Syntax:
Output 10 4 2 6 Explanation: In this example, a Consumer is created to display an integer value. When accept(10) is called, the Consumer receives the value 10 and prints it to the console. The Consumer receives a list and doubles each element. The accept() method executes the operation directly on the provided list. 2. andThen() MethodThe andThen() method combines two Consumer operations into a single Consumer. The current Consumer executes first, followed by the Consumer passed as an argument. Syntax:
Example: Chaining Consumers with andThen()
Output 4 2 6 Explanation: The modify Consumer first doubles each element of the list. After it completes, the display Consumer prints the modified elements. The andThen() method combines these two operations and executes them sequentially. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |