What I have:
SortedSet<Person> ss = new TreeSet<>();
ss.add(new Person("John", "Doe", 20));
ss.add(new Person("Max", "Power", 26));
ss.add(new Person("Bort", "Bort", 30));
ss.add(new Person("Scorpio", "McGreat", 56));
Map<Integer, List<Person>> list = ss.stream().collect(Collectors.groupingBy(p -> p.name.length()));
I need to convert the map to Map<Integer, Person>I know that I need to use flatMap for this purpose, but I do not know how to do this.
What I have tried so far: Get a set of values and a flat map.
Map <Integer, Person> list2 = list.values()
.stream()
.flatMap(f -> f.stream())
.collect(Collectors.groupingBy(f -> f.name.length()); //doesn't work
Question: As far as I understand, Java returns values in the form of lists when creating maps through streams, how can I copy these lists?
Additional Information: Implementation of compareTo
@Override
public int compareTo(Person o) {
int cmp = this.surname.compareTo(o.surname);
if(cmp == 0){
cmp = this.name.compareTo(o.name);
}
return cmp;
}
RENOUNCEMENT:
, , compareTo . , a Map<Key, List<Value>>.
IS: Map<Key,Value>