Java 是否有可用于任何 lambda 的无操作 (NOP) 的方法参考?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29851525/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:42:05  来源:igfitidea点击:

Is there a method reference for a no-op (NOP) that can be used for anything lambda?

javalambdafunctional-programmingjava-8

提问by Ben

This may sounds like a strange question, but is there a way to refer to a standard no-op (aka null operation, null-pattern method, no-operation, do-nothing method) method for a Lambda in Java 8.

这听起来可能是一个奇怪的问题,但是有没有办法在 Java 8 中引用 Lambda 的标准无操作(又名空操作、空模式方法、无操作、无操作方法)方法。

Currently, I have a method that takes a, say, void foo(Consumer<Object>), and I want to give it a no-op, I have to declare:

目前,我有一个方法,比如,,void foo(Consumer<Object>)我想给它一个空操作,我必须声明:

foo(new Consumer<Object>() { 
  public void accept(Object o) { 
    // do nothing 
  }
}

where I would like to be able to do something like:

我希望能够执行以下操作:

foo(Object::null)

instead. Does something like exist?

反而。类似的东西存在吗?

Not sure how that would work with multi-parameter methods -- perhaps this is a deficiency in the lambdas in Java.

不确定这将如何与多参数方法一起使用——也许这是 Java 中 lambdas 的一个缺陷。

采纳答案by fge

This is no deficiency.

这不是不足。

Lambdas in Java are instances of functional interfaces; which, in turn, are abstracted to instances of Java constructs which can be simplified to onesingle abstract method, or SAM.

Java 中的 Lambda 是函数式接口的实例;反过来,它们又被抽象为 Java 结构的实例,这些实例可以简化为一个单一的抽象方法或 SAM。

But this SAM still needs to have a valid prototype. In your case, you want to have a no-op Consumer<T>which does nothing whatever the T.

但是这个SAM仍然需要有一个有效的原型。在您的情况下,您希望有一个无操作Consumer<T>,无论T.

It still needs to be a Consumer<T>however; which means the minimal declaration you can come up with is:

Consumer<T>然而,它仍然需要是一个;这意味着您可以提出的最小声明是:

private static final Consumer<Object> NOOP = whatever -> {};

and use NOOPwhere you need to.

NOOP在需要的地方使用。

回答by Anderson Vieira

In your particular case you could simply do:

在您的特定情况下,您可以简单地执行以下操作:

foo(i -> {});

This means that the lambda expression receives a parameter but has no return value.

这意味着 lambda 表达式接收一个参数但没有返回值。

回答by OldCurmudgeon

Could Function.identity()fit your needs?

Function.identity()满足你的需求吗?

Returns a function that always returns its input argument.

返回一个始终返回其输入参数的函数。

回答by Paul Boddington

If you want a method reference for a method that does nothing, the easiest way is to write a method that does nothing. Notice that in this example I have used Main::doNothingwhen a Consumer<String>is required.

如果你想要一个什么都不做的方法的方法引用,最简单的方法是编写一个什么都不做的方法。请注意,在此示例中,我使用了Main::doNothingwhen aConsumer<String>是必需的。

class Main {

    static void doNothing(Object o) { }

    static void foo(Consumer<String> c) { }

    public static void main(String[] args) {
        foo(Main::doNothing);
    }
}

You could also overload doNothingby providing a version using varargs.

您还可以doNothing通过使用可变参数提供版本来重载。

static void doNothing(Object... o) { }

This signature will accept literally any sequence of parameters (even primitives, as these will get autoboxed). That way you could pass Main::doNothingwhenever the functional interface's method has voidreturn type. For example you could pass Main::doNothingwhen an ObjLongConsumer<Integer>is needed.

这个签名将接受任何参数序列(甚至是基元,因为它们会被自动装箱)。这样你就可以Main::doNothing在函数接口的方法有void返回类型时传递。例如,您可以Main::doNothingObjLongConsumer<Integer>需要时通过。

回答by MNZ

You can have your own NOOP implementation, similar to Function.Identity.

您可以拥有自己的 NOOP 实现,类似于 Function.Identity。

static <T> Consumer<T> NOOP() {
    return t -> {};
}