我有一個模型類如下:public class CCP implements Serializable{ private static final long serialVersionUID = 1L; @Id @Column(name = "p_id") private Integer pId; @Id @Column(name = "c_id") private Integer cId; @Column(name = "priority") private Integer priority;}我有以下要求:轉換List<CCP>成Map<pid, List<cid>>也就是說,我想將 CCP 對象列表轉換為以 pid 為鍵、以關聯 cid 列表為值的映射。我嘗試了以下事情:Map<Integer, List<CCP>> xxx = ccplist.stream() .collect(Collectors.groupingBy(ccp -> ccp.getPId()));但這僅給出了 CCP 的列表。我如何在此處獲取 cid 列表而不是 CCP?
2 回答

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
使用mapping
:
Map<Integer, List<Integer>> xxx = ccplist.stream() .collect(Collectors.groupingBy(CCP::getPId, Collectors.mapping(CCP::getCId, Collectors.toList())));

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
ccplist.stream() .collect(Collectors.groupingBy( CCP::getPId, Collectors.mapping(CCP::getCId, Collectors.toList())));
添加回答
舉報
0/150
提交
取消