I have the list of following objects. Using Stream
API, can I get a User
with the number of frequency it appeared in the List
?
public class User {
string name;
int age;
}
I have the list of following objects. Using Stream
API, can I get a User
with the number of frequency it appeared in the List
?
public class User {
string name;
int age;
}
You can use groupingBy
combined with counting
:
Map<String,Long> countByName =
users.stream()
.collect(Collectors.groupingBy(User::getName,Collectors.counting()));
This gives you the the count for each User
name.