第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用流從 List<Entity> 創(chuàng)建 List<GroupEntity>?

如何使用流從 List<Entity> 創(chuàng)建 List<GroupEntity>?

慕田峪4524236 2023-04-26 16:10:26
我收到一個(gè)實(shí)體列表,我需要將它們按 2 個(gè)字段(periodMonth 和 periodYear)分組并作為新列表返回。為了存儲(chǔ)組,我創(chuàng)建了一個(gè)類 GroupEntity。我必須用來分組的字段不屬于實(shí)體本身,而是屬于一個(gè)名為 EntityPK 的可嵌入類。下面是這些類的代碼:實(shí)體@Entity@Table(name = "Entity")public class Entity {    @EmbeddedId    private EntityPK entityPk;    @Column    private String someValue;實(shí)體PK@Embeddablepublic class EntityPK {    @Column    private String periodMonth;    @Column    private String periodYear;團(tuán)體課public class GroupOfEntity {    private String periodMonth;    private String periodYear;    private List<Entity> entities;我可以遍歷該列表并創(chuàng)建一個(gè)以 periodMonth/Year 為鍵、以 List 為值的地圖,如下所示:Set<GroupEntity> listOfGroupEntity = new HashSet<GroupEntity>();        for(Entity entity: listOfEntity) {            GroupEntity groupEntity = new GroupEntity();            groupEntity.setPeriodMonth(entity.getEntityPk().getPeriodMonth());            groupEntity.setPeriodYear(entity.getEntityPk().getPeriodYear());            Optional<GroupEntity> findFirst = listOfGroupEntity.stream().filter(a -> a.equals(groupEntity)).findFirst();            if(findFirst.isPresent()) {                findFirst.get().getEntities().add(entity);            }else {                listOfGroupEntity.add(groupEntity);            }        }但是我怎么能用流來做到這一點(diǎn)呢?就像是List<GroupEntity> groupEntities     = listOfEntities.stream().collect(Collectors.groupingBy(Entity::getEntityPk::getPeriodMonth,Entity::getEntityPk::getPeriodYear)并使用這些實(shí)體為每個(gè)組創(chuàng)建 GroupEntity 對(duì)象。是否可以?
查看完整描述

2 回答

?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

假設(shè)實(shí)現(xiàn)and ,您首先構(gòu)建,然后EntityPK將其映射到.equalshashCodeMap<EntityPK, List<Entity>>List<GroupOfEntity>

假設(shè) GroupOfEntity有合適的構(gòu)造函數(shù),你會(huì)這樣做:

List<GroupOfEntity> groupEntities = listOfEntities.stream()
        .collect(Collectors.groupingBy(Entity::getEntityPk))
        .entrySet().stream()
        .map(e -> new GroupOfEntity(e.getKey().getPeriodMonth(),
                                    e.getKey().getPeriodYear(),
                                    e.getValue()))
        .collect(Collectors.toList());


查看完整回答
反對(duì) 回復(fù) 2023-04-26
?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

首先,這樣做:

?Map<EntityPK,?List<Entity>>?groupEntities?=
?entities.stream().collect(Collectors.groupingBy(Entity::getEntityPk));

然后從上面的地圖創(chuàng)建 GroupOfEntity。這兩個(gè)步驟也可以結(jié)合起來。

stream這是進(jìn)一步映射和創(chuàng)建的組合步驟GroupOfEntity

List<GroupOfEntity>?groupEntities
????????=?entities.stream().collect(Collectors.groupingBy(Entity::getEntityPk)).entrySet()
????????.stream().map(e?->?new?GroupOfEntity(e.getKey().getPeriodMonth(),?e.getKey().getPeriodYear(),?e.getValue()))
????????.collect(Collectors.toList());
查看完整回答
反對(duì) 回復(fù) 2023-04-26
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)