Java: groupingBy subvalue as value
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person>
and I like to use streams.
Now I want to generate a Map<FirstName, List<LastName>>
in order to group people with the same first name. How do I go about this without writing much code? My approach so far is
personList
.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
person -> person.getLastName() // this seems to be wrong
));
but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>()
as initial value and then aggregate to it by putting elements inside?
java java-stream
add a comment |Â
up vote
6
down vote
favorite
Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person>
and I like to use streams.
Now I want to generate a Map<FirstName, List<LastName>>
in order to group people with the same first name. How do I go about this without writing much code? My approach so far is
personList
.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
person -> person.getLastName() // this seems to be wrong
));
but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>()
as initial value and then aggregate to it by putting elements inside?
java java-stream
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person>
and I like to use streams.
Now I want to generate a Map<FirstName, List<LastName>>
in order to group people with the same first name. How do I go about this without writing much code? My approach so far is
personList
.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
person -> person.getLastName() // this seems to be wrong
));
but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>()
as initial value and then aggregate to it by putting elements inside?
java java-stream
Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person>
and I like to use streams.
Now I want to generate a Map<FirstName, List<LastName>>
in order to group people with the same first name. How do I go about this without writing much code? My approach so far is
personList
.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
person -> person.getLastName() // this seems to be wrong
));
but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>()
as initial value and then aggregate to it by putting elements inside?
java java-stream
java java-stream
edited 2 hours ago
asked 2 hours ago
Phil
1,75811628
1,75811628
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
personList.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
You are looking for a downstream collector with groupingBy
add a comment |Â
up vote
5
down vote
This should work for you :
Map<String, List<String>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
add a comment |Â
up vote
0
down vote
I think you are looking for something like this:
Map<String, Map<String, List>> map = personList.stream()
.collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));
The double grouping gives you a map of a map. That's the trick.
1
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
1
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
personList.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
You are looking for a downstream collector with groupingBy
add a comment |Â
up vote
5
down vote
accepted
personList.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
You are looking for a downstream collector with groupingBy
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
personList.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
You are looking for a downstream collector with groupingBy
personList.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
You are looking for a downstream collector with groupingBy
edited 56 mins ago
answered 2 hours ago
Eugene
64.4k989152
64.4k989152
add a comment |Â
add a comment |Â
up vote
5
down vote
This should work for you :
Map<String, List<String>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
add a comment |Â
up vote
5
down vote
This should work for you :
Map<String, List<String>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
add a comment |Â
up vote
5
down vote
up vote
5
down vote
This should work for you :
Map<String, List<String>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
This should work for you :
Map<String, List<String>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getFirstName,
Collectors.mapping(Person::getLastName, Collectors.toList())));
answered 2 hours ago
nullpointer
32.1k1064133
32.1k1064133
add a comment |Â
add a comment |Â
up vote
0
down vote
I think you are looking for something like this:
Map<String, Map<String, List>> map = personList.stream()
.collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));
The double grouping gives you a map of a map. That's the trick.
1
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
1
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
add a comment |Â
up vote
0
down vote
I think you are looking for something like this:
Map<String, Map<String, List>> map = personList.stream()
.collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));
The double grouping gives you a map of a map. That's the trick.
1
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
1
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I think you are looking for something like this:
Map<String, Map<String, List>> map = personList.stream()
.collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));
The double grouping gives you a map of a map. That's the trick.
I think you are looking for something like this:
Map<String, Map<String, List>> map = personList.stream()
.collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));
The double grouping gives you a map of a map. That's the trick.
answered 2 hours ago
Perimosh
158211
158211
1
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
1
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
add a comment |Â
1
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
1
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
1
1
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
Thank you, but the value was supposed to be a simple list, not another map.
â Phil
2 hours ago
1
1
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
I see it. I voted up nullpointer's response. That's the one.
â Perimosh
2 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52879392%2fjava-groupingby-subvalue-as-value%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password