Java函数式接口带有一个参数,能应用于无参数方法吗?

huangapple go评论67阅读模式
英文:

Java Functional Interface with one argument can apply to no argument method?

问题

参考资料


这是 Predicate 函数式接口的抽象方法。它接受一个参数 T t 并返回 boolean

boolean test(T t)
    在给定参数上评估此 predicate

但是,在这个例子中,String::isEmpty 并不接受任何参数,但它是 String 的实例方法。

Predicate<String> p = String::isEmpty;

我可以推断在 Java 中,这种类型的方法可以应用于具有一个参数的函数式接口。

并且,是否有关于具有一个参数且函数本身也有一个参数的函数式接口的其他示例?

英文:

References


This is Predicate Functional Interface's abstract method. It gets one argument T t and return boolean.

boolean 	test​(T t) 	
    Evaluates this predicate on the given argument.

But, in this example, String::isEmpty does not get any argument but it is instance method of String.

Predicate&lt;String&gt; p = String::isEmpty;

I can infer that in Java, this type of method can apply to Functional Interface with one argument.

And, is there any examples of one argument Functional Interface with the function has one argument not like upper example?

答案1

得分: 0

你可以使用Function<T, R>,其中R是返回类型,T是参数类型。
因此可以这样使用:R result = fnc.apply(k),其中k的类型为T

Function<Integer, List<Integer>> fnc = n -> new ArrayList<>(List.of(n));
List<Integer> list = fnc.apply(23);
System.out.println(list);

输出:

[23]

但是你也可以像你的示例中一样做:

Function<Integer, List<Integer>> fnc2 = fnc::apply;
fnc2.apply(44); // 返回一个包含 44 的列表

额外细节

考虑以下类,其中包含一个静态方法和一个实例方法。

class TestClass {
    public int prod(int a) {
        return a * 33;
    }

    public static int mult(int a) {
        return a * 44;
    }
}

要为静态方法创建 Lambda,请执行以下操作:

// 使用 UnaryOperator,因为返回类型和参数类型相同
UnaryOperator<Integer> fnc = TestClass::mult;
int result = fnc.apply(10); // 返回 440

现在来创建实例方法的版本。

UnaryOperator<Integer> inst = tc::prod;
inst.apply(10);
result = inst.apply(10); // 返回 330

现在是一个特殊情况,使用静态类型引用到实例方法。

// 这需要两个参数:实例和方法的参数。
BiFunction<TestClass, Integer, Integer> stat = TestClass::prod;
result = stat.apply(tc, 12); // 结果为 396
英文:

You can do Function&lt;T, R&gt; where R is the return type and T is the argument type.<br>
So R result = fnc.apply(k) where k is of type T.

Function&lt;Integer, List&lt;Integer&gt;&gt; fnc =  n-&gt; new ArrayList&lt;&gt;(List.of(n));
List&lt;Integer&gt; list = fnc.apply(23);
System.out.println(list);

prints

[23]

But you can also do this, just like in your example.

Function&lt;Integer, List&lt;Integer&gt;&gt; fnc2 = fnc::apply;
fnc2.apply(44); // returns a list which contains 44

EXTRA DETAILS

Consider the following class. One static and one instance method.

class TestClass {
	public int prod(int a) {
		return a * 33;
	}
	
	public static int mult(int a) {
		return a * 44;
	}
}

To create a Lambda for the static, do the following:

// use unaryOperator since return type are argument are the same
UnaryOperator&lt;Integer&gt; fnc = TestClass::mult;
int result = fnc.apply(10); // returns 440

Now do an instance version.

UnaryOperator&lt;Integer&gt; inst = tc::prod;
inst.apply(10);
result = inst.apply(10); // returns 330

Now a special case using a static type reference.<br>
to an instance method.


// This requires 2 arguments. The instance
// and the argument to the method.a

BiFunction&lt;TestClass, Integer, Integer&gt; stat = TestClass::prod
result = stat.apply(tc, 12); // result is 396

</details>



# 答案2
**得分**: 0

以下是翻译好的内容:

有三种形式的谓词(Predicate)可用。以下是在字符串上针对 `isEmpty` 使用的形式:

1- 一个正式的函数式接口:

    Predicate<String> somePredicate = new Predicate<String>() {
        @Override
        public boolean test(String s) {
            return s.isEmpty();
        }
    };

2- 一个lambda表达式:

    (String s) -> s.isEmpty()

3- 一个方法引用

    String::isEmpty

它们具有相同的功能,每个都可以在需要 `Predicate<String>` 的情况下传递。

如果底层函数不是对象上的方法,则不适用于第三种情况。

<details>
<summary>英文:</summary>

There are three forms that a Predicate can take.  Here they are for `isEmpty` on a String:

1- A formal functional interface:

    Predicate&lt;String&gt; somePredicate = new Predicate&lt;String&gt;() {
        @Override
        public boolean test(String s) {
            return s.isEmpty();
        }
    };

2- A lambda:

    (String s) -&gt; s.isEmpty()

3- A method reference

    String::isEmpty

These have identical function and can each be passed when a `Predicate&lt;String&gt;` is required.

The third doesn&#39;t apply if the underlying function is not a method on an object.


</details>



huangapple
  • 本文由 发表于 2020年10月21日 09:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64455453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定