Convert map <Key, List <Value >> to map <Key, value>

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>

+4
1

. , , . , . , , , , / toMap, ( IMO).

, :

Map<Integer, Person> list = 
    ss.stream()
      .collect(Collectors.toMap(p -> p.surname.length(), 
                                p -> p, 
                                (p1, p2) -> {throw new IllegalStateException("Duplicate key with length " + p1.surname.length());}));

:

{3=(John, Doe, 20), 4=(Bort, Bort, 30), 5=(Max, Power, 26), 7=(Scorpio, McGreat, 56)}
+3

All Articles