Java8 streams : Transpose map with values as list

前端 未结 4 2124
情歌与酒
情歌与酒 2020-12-03 08:45

I have map with key as String and value as List. List can have 10 unique values. I need to convert this map with key as Integer and value as List. Example as below :

4条回答
  •  萌比男神i
    2020-12-03 09:19

    You can Achieve in this way

    Let suppose I have Person class with Gender and Age . I want to get it in this form

    Map> 
    

    I would write simply

    Map> map =  personList.stream()
    
                               .collect(Collectors.groupingBy(Person::getGender));
    

    it will get me something like below (one key against multiple values )

    key:MALE
    age31sexMALE
    age28sexMALE
    
    key:FEMALE
    age40sexFEMALE
    age44sexFEMALE
    

提交回复
热议问题