Junior — Middle
What type of interface is used in the forEach method for processing collection elements?
sobes.tech AI
Answer from AI
In Java, the forEach method of collections uses the functional interface Consumer<T>. This interface contains a single abstract method accept(T t), which takes an element of the collection and performs some action with it.
Example of usage:
List<String> list = Arrays.asList("a", "b", "c");
list.forEach(item -> System.out.println(item));
Here, the lambda expression corresponds to the Consumer<String> interface, accepting each element and printing it.