Index
Question: Having a hard time with actionFct in forEach


This is a great question because it allows us to learn some general techniques.

In Java, we know object types. This is really helpful in determining object usage.

For the question, we are given this method header:

public void forEach(Consumer<? super E> actionFct) {

That small header tells us a bunch, including:


Awesome, we're now on our way, we just need to understand usage of "Consumer".

Via internet search "java Consumer", and then going to probably the first search result:


The description tells us this:

This is a functional interface whose functional method is "accept(Object)", and the method header for "accept" tells us there is not a return value (i.e., void).

Bingo, this gives us the needed "object usage".

Thus our usage is:

actionFct.accept(someArgument)

Note that this is a general technique for oo languages like Java, JavaScript, etc. Look up the data type (e.g., class) and look up individual methods to see how they are used. And the beauty is we don't have to weed through volumes of code, but rather only need to look at method headers, and possibly a short description, which tell us all we need to know about usage.

As another example, let us say we have a method parameter data type of "Function". Thus, we do an internet search:

java Function

And following the same general steps as above, we would learn that a Function object works like this:

myOutput = myFunction.apply(myInput)