I have a class Person, which stores the code of each person and a list of friends. I'm trying to get the code of the person with the highest number of friends, but cannot figure it out. The methods getFriends and getCode are provided by the Person class.
How can I return a string from the stream?
HashMap<String,Person> persons = new HashMap<>();
public String personWithLargestNumberOfFriends() {
return persons.values().stream()
.sorted(comparing(p -> ((Person)p).getFriends().size()).reversed())
.limit(1)
.forEach(p -> ((Person)p).getCode());
}
forEach, solutions will arise…