英文:
Java Functional Interface with one argument can apply to no argument method?
问题
参考资料
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/function/package-summary.html
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#isEmpty()
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/function/ToIntFunction.html
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Integer.html#intValue()
这是 Predicate
函数式接口的抽象方法。它接受一个参数 T t
并返回 boolean
。
boolean test(T t)
在给定参数上评估此 predicate。
但是,在这个例子中,String::isEmpty
并不接受任何参数,但它是 String
的实例方法。
Predicate<String> p = String::isEmpty;
我可以推断在 Java 中,这种类型的方法可以应用于具有一个参数的函数式接口。
并且,是否有关于具有一个参数且函数本身也有一个参数的函数式接口的其他示例?
英文:
References
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/function/package-summary.html
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#isEmpty()
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/function/ToIntFunction.html
- https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Integer.html#intValue()
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<String> 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<T, R>
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<Integer, List<Integer>> fnc = n-> new ArrayList<>(List.of(n));
List<Integer> list = fnc.apply(23);
System.out.println(list);
prints
[23]
But you can also do this, just like in your example.
Function<Integer, List<Integer>> 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<Integer> fnc = TestClass::mult;
int result = fnc.apply(10); // returns 440
Now do an instance version.
UnaryOperator<Integer> 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<TestClass, Integer, Integer> 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<String> somePredicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.isEmpty();
}
};
2- A lambda:
(String s) -> s.isEmpty()
3- A method reference
String::isEmpty
These have identical function and can each be passed when a `Predicate<String>` is required.
The third doesn't apply if the underlying function is not a method on an object.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论