I'm trying to come up with a more succinct expression for the "keyMapper" function parameter in the following "Collectors.toMap()" call:
我正试图在以下的“Collectors.toMap()”中为“keyMapper”函数参数提供一个更简洁的表达式。
List<Person> roster = ...;
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(
new Function<Person, String>() {
public String apply(Person p) { return p.getLast(); }
},
Function.<Person>identity()));
It seems that I should be able to inline it using a lambda expression, but I cannot come up with one that compiles. (I'm quite new to lambdas, so that's not much of a surprise.)
似乎我应该能够使用lambda表达式内联它,但是我不能找到编译的表达式。(我对lambdas很陌生,所以这没什么好惊讶的。)
Thanks.
谢谢。
--> Update:
- - >更新:
As noted in the accepted answer
正如所接受的答案所指出的那样。
Person::getLast
is what I was looking for, and is something I had tried. However, the BETA_8 nightly build of Eclipse 4.3 was the problem -- it flagged that as wrong. When compiled from the command-line (which I should have done before posting), it worked. So, time to file a bug with eclipse.org.
是我一直在寻找的,也是我曾经尝试过的。然而,Eclipse 4.3的BETA_8夜间构建是一个问题——它标记出了错误。当从命令行(我应该在发布之前完成)编译时,它起作用了。所以,是时候向eclipse.org提交一个bug了。
Thanks.
谢谢。
3 个解决方案
#1
124
You can use a lambda:
你可以用
Collectors.toMap(p -> p.getLast(), Function.identity())
or, more concisely, you can use a method reference using ::
:
或者,更简洁地说,您可以使用以下方法引用:::
Collectors.toMap(Person::getLast, Function.identity())
and instead of Function.identity
, you can simply use the equivalent lambda:
而不是函数。等式,你可以简单地用等价的
Collectors.toMap(Person::getLast, p -> p)
If you use Netbeans you should get hints whenever an anonymous class can be replaced by a lambda.
如果您使用Netbeans,您应该在一个匿名类可以被lambda替换时得到提示。
#2
20
List<Person> roster = ...;
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(p -> p.getLast(), p -> p)
);
that would be the translation, but i havent run this or used the API. most likely you can substitute p -> p, for Function.identity(). and statically import toMap(...)
这将是翻译,但我没有运行这个或使用这个API。很有可能你可以用p -> p来代替函数。identity()。和静态导入toMap(…)
#3
4
We can use an optional merger function also in case of same key collision. For example, If two or more persons have the same getLast() value, we can specify how to merge the values. If we not do this, we could get IllegalStateException. Here is the example to achieve this...
我们还可以使用一个可选的合并函数,以防止相同的键碰撞。例如,如果两个或多个person拥有相同的getLast()值,我们可以指定如何合并这些值。如果我们不这样做,我们就会得到非法的国家。这里有一个例子来实现这个…
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(p -> p.getLast(),
p -> p,
(person1, person2) -> person1+";"+person2)
);
#1
124
You can use a lambda:
你可以用
Collectors.toMap(p -> p.getLast(), Function.identity())
or, more concisely, you can use a method reference using ::
:
或者,更简洁地说,您可以使用以下方法引用:::
Collectors.toMap(Person::getLast, Function.identity())
and instead of Function.identity
, you can simply use the equivalent lambda:
而不是函数。等式,你可以简单地用等价的
Collectors.toMap(Person::getLast, p -> p)
If you use Netbeans you should get hints whenever an anonymous class can be replaced by a lambda.
如果您使用Netbeans,您应该在一个匿名类可以被lambda替换时得到提示。
#2
20
List<Person> roster = ...;
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(p -> p.getLast(), p -> p)
);
that would be the translation, but i havent run this or used the API. most likely you can substitute p -> p, for Function.identity(). and statically import toMap(...)
这将是翻译,但我没有运行这个或使用这个API。很有可能你可以用p -> p来代替函数。identity()。和静态导入toMap(…)
#3
4
We can use an optional merger function also in case of same key collision. For example, If two or more persons have the same getLast() value, we can specify how to merge the values. If we not do this, we could get IllegalStateException. Here is the example to achieve this...
我们还可以使用一个可选的合并函数,以防止相同的键碰撞。例如,如果两个或多个person拥有相同的getLast()值,我们可以指定如何合并这些值。如果我们不这样做,我们就会得到非法的国家。这里有一个例子来实现这个…
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(p -> p.getLast(),
p -> p,
(person1, person2) -> person1+";"+person2)
);