API中的奇怪语法“String :: concat” [英] Odd syntax in API "String::concat"
问题描述
我正在查看使用1.8对Java SE API所做的一些更改,以及查看新方法时的一些更改 Map.merge 它显示了如何将它与行一起使用的示例
I was looking at some of the changes made to the Java SE API with 1.8, and I when looking at the new method Map.merge it shows an example of how to use it with the line
map.merge(key, msg, String::concat)
我理解如何使用lambda表达式来创建匿名功能接口,但这似乎使用了一个方法作为BiFunction。我喜欢理解和使用晦涩的java语法,但我无法在任何地方找到任何提及。
I understand how to use a lambda expressions to create anonymous functional interfaces, but this seems to use a method as a BiFunction. I like to understand and use obscure java syntaxes, but I can't find any mention of this one anywhere.
推荐答案
String :: concat
是对<$ c的引用$ c> concat() String类的方法。
String::concat
is a reference to the concat()
method of the String class.
A BiFunction
是一个带有单个方法的函数接口 apply
接受两个参数(第一个类型为 T
,第二个类型为 U
),并返回类型 R
的结果(换句话说,接口 BiFunction< T,U,R>
有一个方法 R apply(T t,U u)
)。
A BiFunction
is a functional interface with a single method apply
that accepts two parameters (the first of type T
and the second of type U
), and returns a result of type R
(in other words, the interface BiFunction<T,U,R>
has a method R apply(T t, U u)
).
map.merge
期望 BiFunction<?超级V,?超级V,?扩展V>
作为第三个参数,其中 V
是 Map
的值。如果您的 Map
带有 String
值,则可以使用任何接受两个的方法String
参数并返回字符串
。
map.merge
expects a BiFunction<? super V,? super V,? extends V>
as the third parameter, where V
is the value of the Map
. If you have a Map
with a String
value, you can use any method that accepts two String
parameters and returns a String
.
字符串: :concat
满足这些要求,这就是为什么它可以在 map.merge
中使用。
String::concat
satisfies these requirements, and that's why it can be used in map.merge
.
满足这些要求的原因需要解释:
The reason it satisfies these requirements requires an explanation :
String :: concat
的签名是 public String concat(String str)
。
这可以看作是一个带有两个String类型参数的函数(这个
,调用此方法的实例,以及 str
参数)和String类型的结果。
This can be seen as a function with two parameters of type String (this
, the instance for which this method is called, and the str
parameter) and a result of type String.
这篇关于API中的奇怪语法“String :: concat”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!