如何使用流从 List<Entity> 创建 List<GroupEntity>?

我收到一个实体列表,我需要将它们按 2 个字段(periodMonth 和 periodYear)分组并作为新列表返回。


为了存储组,我创建了一个类 GroupEntity。


我必须用来分组的字段不属于实体本身,而是属于一个名为 EntityPK 的可嵌入类。


下面是这些类的代码:


实体


@Entity

@Table(name = "Entity")

public class Entity {


    @EmbeddedId

    private EntityPK entityPk;


    @Column

    private String someValue;

实体PK


@Embeddable

public class EntityPK {


    @Column

    private String periodMonth;


    @Column

    private String periodYear;


团体课


public class GroupOfEntity {


    private String periodMonth;


    private String periodYear;


    private List<Entity> entities;

我可以遍历该列表并创建一个以 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);

            }

        }

但是我怎么能用流来做到这一点呢?


就像是


List<GroupEntity> groupEntities 

    = listOfEntities.stream().collect(Collectors.groupingBy(Entity::getEntityPk::getPeriodMonth,Entity::getEntityPk::getPeriodYear)


并使用这些实体为每个组创建 GroupEntity 对象。


是否可以?


慕田峪4524236
浏览 103回答 2
2回答

烙印99

假设实现and&nbsp;,您首先构建,然后EntityPK将其映射到.equalshashCodeMap<EntityPK, List<Entity>>List<GroupOfEntity>假设&nbsp;GroupOfEntity有合适的构造函数,你会这样做:List<GroupOfEntity>&nbsp;groupEntities&nbsp;=&nbsp;listOfEntities.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.groupingBy(Entity::getEntityPk)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.entrySet().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(e&nbsp;->&nbsp;new&nbsp;GroupOfEntity(e.getKey().getPeriodMonth(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.getKey().getPeriodYear(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.getValue())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

白衣染霜花

首先,这样做: Map<EntityPK, List<Entity>> groupEntities =  entities.stream().collect(Collectors.groupingBy(Entity::getEntityPk));然后从上面的地图创建 GroupOfEntity。这两个步骤也可以结合起来。stream这是进一步映射和创建的组合步骤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());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java