开发者

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

Because the JDK doesn't contain them, a zillion of 3rd party libraries contain Action or Func interfaces, that look something like:

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?


You can use Runnable (void) or Callable (returning a value). But as you note, packages often create their own. Part of the reason is to have a more meaningful name than run or call. You should note that Java does not have C#-style delegates, nor does it currently have a way to implicitly convert between a method and an instance of an interface.

Note that neither of the above interfaces allows parameters. If you need to, you will need a different solution.

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


While I don't know of any interface that matches your Action<T> signature (possibly partly because such a signature mandates that instances of Action produce side effects), Guava has a Function interface that matches the second signature, plus lots of Collection and Iterable related utilities that make use of Functions to do lazy mapping (called transform in Guava). It's also just a really great general-purpose Java library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜