What is the de facto standard for Action / Func classes?

Since the JDK does not contain them, the zillion third-party libraries contain Action or Func interfaces that look something like this:

public interface Action<T> { void doSomething(T item); } public interface Func<TResult, TInput> { TResult doSomething(TInput input); } 

What is the de facto or most commonly used standard for this?

+4
source share
2 answers

You can use Runnable (void) or Callable (return value). But, as you have noticed, packages often create their own. Part of the reason is to have a more meaningful name than run or call . You should notice that Java has no C # style delegates, and it has no way to implicitly convert a method and an instance of an interface.

Please note that none of the above interfaces allow parameters. If you need, you will need a different solution.

EDIT: I have used Apache Functor in the past, and it has some of these interfaces (like UnaryProcedure and UnaryFunction for the ones in question). However, I would still consider creating my own, especially if you don't need anything else from the Functor (e.g. algorithm or adapter )

+3
source

As long as I don’t know any interface that matches your Action<T> signature (perhaps partly because such a signature requires that Action instances cause side effects), Guava has a Function interface that matches the second signature, plus a lot of Collection and Iterable related utilities that use Function to create a lazy mapping (called transform in Guava). It is also just a great general purpose Java library.

+1
source

All Articles