Sheldon Sheldon - 1 month ago 8
Java Question

What is the correct way to use lambda expression in .stream() and in Collectors?

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


This is EmailAddress an object class which take in two parameters(alias and email_address)

enter image description here

enter image description here

Answer

I think this will do what you want:

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]);
Comments