Java equivalent of .NET Action<T> and Func<T,U>, etc [duplicate]

谁说胖子不能爱 提交于 2019-12-08 15:09:22

问题


Are there any standard generic "callback" or "function/method" types in Java, like System.Action<T> or System.Func<T,U> in .NET?

In my concrete case, I need a class that wraps a method that takes one (generic) parameter of type T and returns nothing (i.e. void).

Yes, it's easy enough to create such a class/interface for myself, but I'd prefer a standard library class if there is one.


回答1:


I don't believe there's anything in the standard library like this, no (at the time of writing, 2010). Other libraries have them - things like Guice's Provider<T> and Guava's Function<F, T>, Supplier<T> and Predicate<T>... so if you're using libraries already, you might want to look into what they provide in this direction.

(EDIT: As noted, those are interfaces which return things - so similar to Func<T, U> but not similar to Action<T>.)




回答2:


Java standard library doesn't provide any such classes.

You can use Functional Java library which provides fj.F1<T, fj.Unit> which is basically equivalent of System.Action<T> from .NET.



来源:https://stackoverflow.com/questions/3244582/java-equivalent-of-net-actiont-and-funct-u-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!