首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >stream.distinct检查列表中是否存在重复值的问题

stream.distinct检查列表中是否存在重复值的问题
EN

Stack Overflow用户
提问于 2022-10-29 13:53:57
回答 1查看 49关注 0票数 0

我的服务有问题。此服务包含一个add方法,该方法接收作为参数的表单。

代码语言:javascript
运行
复制
public Cheminement add(final CheminementForm cheminementForm) throws BadRequestException {
        if(cheminementForm == null){
            log.error("Cheminement can not be null");
            throw new BadRequestException("CheminementForm  can not be null");
        }else if (Objects.isNull(cheminementForm.getName())){
            log.error("All fields must be filled.");
            throw new BadRequestException("All fields must be filled.");
        }
        Cheminement cheminement = Cheminement.builder().disable(false).name(cheminementForm.getName()).build();
        List<CheminementEtape> cheminementEtapeList = new ArrayList<>();

        if(cheminementForm.getPositionsPoste().stream().distinct().count() != cheminementForm.getPositionsPoste().size()){
            throw new BadRequestException("Cannot have same positions");
        }
        for(int i=0; i<cheminementForm.getEtapes().size(); i++){
            if(cheminementForm.getPositionsPoste().get(i) < 0 ){
                throw new BadRequestException("position cannot be null");
            }
            cheminementEtapeList.add(CheminementEtape.builder().cheminement(cheminement).etape(cheminementForm.getEtapes().get(i)).positionPoste(cheminementForm.getPositionsPoste().get(i)).disable(false).build());
        }
        cheminementRepository.save(cheminement);
        cheminementEtapeService.add(cheminementEtapeList);

        return cheminement;
    }

本表格如下:

代码语言:javascript
运行
复制
@Data
public class CheminementForm {
    @NotNull(message = "{cheminement.form.name.notEmpty}")
    @Size(min=2, max=30)
    private String name;

    @NotNull(message = "{cheminementEtape.form.etape.notEmpty}")
    private List<Etape> etapes;


    @NotNull(message = "{cheminementEtape.form.positionPoste.notEmpty}")
    private List<Integer> positionsPoste;
}

包含整数列表和路径列表。我有一个视图,它包含一个html表单,并将一个POST方法返回给一个调用此服务的控制器。

因此,在这个服务中,add方法接受表单并(通过存储库)添加它。到目前为止,一切正常。

但是,我想添加一个条件:检查职位列表不包含相同的值。我添加了条件,但不幸的是,即使没有重复的值,它也总是返回BadRequestException。我不明白。

为了检查是否存在重复项,我使用了stream.distinct.count,它应该等于我的列表。

EN

回答 1

Stack Overflow用户

发布于 2022-11-03 08:38:39

我发现了问题。当我没有在视图中检查空值时,我的列表包含空值。因此,在条件出现之前,我必须从列表中删除所有的空值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74245896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档