I have the following code which group the users with same age and highest score among them.
I have now instead of Map<Person, List<String>> one object called Person and in the person class there Map Map<String,Double> nameScoreTogether;. I need to store the output in the map of the person object with its informations(name and the corresponding score), so how can i change the code accordingly ?
Input: In data type of Map<Person, String>:
{Person has at Age: 12 (Score: 50)
=alex,
Person has at Age: 16 (Score: 50)
=miki,
Person has at Age: 5 (Score: 100)
=shi,
Person has at Age: 4 (Score: 50)
=rafi,
Person has at Age: 1 (Score: 50)
=sharbel,
Person has at Age: 5 (Score: 0)
=thomas,
Person has at Age: 14 (Score: 60)
=thomy,
Person has at Age: 14 (Score: 50)
=angelos,
Person has at Age: 11 (Score: 50)
=musti,
Person has at Age: 11 (Score: 100)
=aloo,
Person has at Age: 2 (Score: 50)
=evi}
The Expected output is:
Person(score=50.0, age=1) - [sharbel=50.0]
Person(score=100.0, age=11) - [aloo=100.0, musti=50.0]
Person(score=50.0, age=12) - [Alex=50.0]
Person(score=60.0, age=14) - [thomy=60.0, angelos=50.0]
Person(score=50.0, age=2) - [evi=50.0]
Person(score=100.0, age=5) - [shi=100.0, Thomas=5.0]
Person(score=50.0, age=4) - [rafi=50]
Person(score=50.0, age=16) - [miki=50]
Try code: but now I have List<Person> which has Map<String,Double>
Map<Person, List<String>> result = origin.entrySet().stream()
.collect(Collectors.groupingBy(e -> e.getKey().getAge())).entrySet().stream()
.collect(Collectors.toMap(
e -> e.getValue().stream()
.map(Map.Entry::getKey)
.max(Comparator.comparing(Person::getScore))
.get(),
e -> e.getValue().stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList()))
);
Class Person:
public class Person {
int Age;
int lineScoreMax;
Map<String, Double> nameScoreTogether;
}
Map<Person, String>and you want to transform it toMap<Person, List<String>>?Person has at Age: 1 (Score: 50). (Score: 50) =sharbel,Why there are two Score in this line?