在.stream()和收集器中使用lambda表达式的正确方法是什么?

时间:2016-11-03 11:17:17

标签: java lambda java-8 java-stream

   Map<String, EmailAddress> mapAliasAddress;
        mapAliasAddress = Files.lines(Paths.get("emails.csv"))
                .map(s->s.split(","))
                .collect(Collectors.groupBy(s->s[1], new EmailAddress(s->s[1],s->[0])));// first is the alias, second is the email address

这是EmailAddress一个对象类,它接受两个参数(别名和email_address)

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为这会做你想做的事情:

Map<String, EmailAddress> mapAliasAddress;
mapAliasAddress = Files.lines(Paths.get("emails.csv"))
            .map(s->s.split(","))
            .collect(Collectors.toMap(s -> s[1], s -> new EmailAddress(s[1], s[0]);