有多个参数的 Java 8 Consumer 在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24779221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the Java 8 Consumer with more than one argument?
提问by Miguel Gamboa
Such as in .Net, which provides several implementations of the Action
delegate (equivalent to Java Consumer
functional interface) with different number and type of arguments, I was expecting that Java 8 provides some way of specifying a Consumer
with more than one argument of different types.
例如在 .Net 中,它提供了多种具有不同数量和类型参数的Action
委托实现(相当于 JavaConsumer
函数式接口),我期待 Java 8 提供某种方法来指定Consumer
具有多个不同类型参数的 a。
I know that in Java we cannot define different types with the same name that just differ in the generic type parameters, but there would be nice fluent alternatives to provide a multi-argument Consumer
.
我知道在 Java 中,我们不能定义具有相同名称的不同类型,只是泛型类型参数不同,但是会有很好的流畅替代方案来提供多参数Consumer
.
Is there any easy way to do it, which does not require defining a new functional interface?
有没有什么简单的方法可以做到,不需要定义新的功能接口?
采纳答案by Lev
For 3 and more arguments you could use curried(http://en.wikipedia.org/wiki/Currying) functions with last consumer:
对于 3 个或更多参数,您可以对最后一个消费者使用 curried( http://en.wikipedia.org/wiki/Currying) 函数:
Function<Double, Function<Integer, Consumer<String>>> f = d -> i -> s -> {
System.out.println("" + d+ ";" + i+ ";" + s);
};
f.apply(1.0).apply(2).accept("s");
Output is:
输出是:
1.0;2;s
It's enough to have a function of one argument to express function of any number of arguments: https://en.wikipedia.org/wiki/Currying#Lambda_calculi
有一个参数的函数来表达任意数量参数的函数就足够了:https: //en.wikipedia.org/wiki/Currying#Lambda_calculi
回答by SimY4
By default you're limited with only java.util.function.Consumer
and java.util.function.BiConsumer
. They are sufficient for current java streaming API. But you can create your own functional interfaces that will receive as many arguments as you like and use it in your own custom APIs.
默认情况下,您只能使用java.util.function.Consumer
和java.util.function.BiConsumer
。它们对于当前的 java 流 API 来说已经足够了。但是您可以创建自己的函数式接口,接收任意数量的参数并在您自己的自定义 API 中使用它。
回答by Peter Lawrey
Is there any easy way to do it, which does not require defining a new functional interface?
有没有什么简单的方法可以做到,不需要定义新的功能接口?
An object can contain any number of other objects. The Streams API is designed to stream just one object at a time, and if you need more you would wrap these in an object which holds them all.
一个对象可以包含任意数量的其他对象。Streams API 旨在一次只传输一个对象,如果您需要更多对象,您可以将它们包装在一个包含所有对象的对象中。
e.g.
例如
Map<String, Long> map = new HashMap<>();
// each entry has a key and a value as Map.Entry<String, Long>
Map<Long, String> revMap = map.entrySet().stream()
.collect(groupingBy(Entry::getValue(), HashMap::new, Entry::getKey));
回答by Mark Lackey
It's pretty easy to define Functional Interfaces for yourself. In the project I am currently working on, I have found several occasions where passing a method as an argument has allowed me to keep my code nice and DRY. Here are some functional interfaces I've defined:
为自己定义函数式接口非常容易。在我目前正在进行的项目中,我发现有几次将方法作为参数传递使我的代码保持良好和干燥。以下是我定义的一些功能接口:
@FunctionalInterface
public interface CheckedVoidFunction {
void apply() throws Exception;
}
@FunctionalInterface
public interface VoidFunction {
void apply();
}
@FunctionalInterface
public interface SetCategoryMethod {
void accept(ObservableList<IRegionWithText> list, Document document, String pattern);
}
Here are a method that accepts these kinds of arguments:
这是一个接受这些类型参数的方法:
private void loadAnnotations(String annotationType, VoidFunction afterLoadFunction, List<Path> documentPaths) {
And here is where I call these methods with different methods as arguments:
这里是我用不同的方法作为参数调用这些方法的地方:
loadAnnotations(DocumentReader.REDACTION_ANNOTATION_TYPE, this::afterLoadRedactions, documentPaths);
loadAnnotations(DocumentReader.HIGHLIGHT_ANNOTATION_TYPE, this::afterLoadHighlights, documentPaths);
There may be better ways to do this, but I thought I'd share what has been useful for me.
可能有更好的方法来做到这一点,但我想我会分享对我有用的东西。