Java Collectors.toMap() keyMapper —— 更简洁的表达?

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

Collectors.toMap() keyMapper -- more succinct expression?

javacollectionslambdajava-8java-stream

提问by pfurbacher

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 表达式内联它,但我无法想出一个可以编译的。(我对 lambda 很陌生,所以这并不奇怪。)

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 提交错误了。

Thanks.

谢谢。

采纳答案by assylias

You can use a lambda:

您可以使用 lambda:

Collectors.toMap(p -> p.getLast(), Function.identity())

or, more concisely, you can use a method referenceusing :::

或者,更简洁,您可以使用方法参考使用::

Collectors.toMap(Person::getLast, Function.identity())

and instead of Function.identity, you can simply use the equivalent lambda:

而不是Function.identity,您可以简单地使用等效的 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 替换时得到提示。

回答by aepurniet

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 代替 Function.identity()。并静态导入 toMap(...)

回答by KayV

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...

我们也可以使用可选的合并功能,以防相同的密钥冲突。例如,如果两个或更多人具有相同的 getLast() 值,我们可以指定如何合并这些值。如果我们不这样做,我们可能会得到 IllegalStateException。这是实现此目的的示例...

Map<String, Person> map = 
roster
    .stream()
    .collect(
        Collectors.toMap(p -> p.getLast(),
                         p -> p,
                         (person1, person2) -> person1+";"+person2)
    );