使用Java 8检查数组中是否存在元素

使用Java 8检查数组中是否存在元素,java,arrays,java-8,Java,Arrays,Java 8,我有一个DTO,它包含一个列表,我在DAO中添加或删除了一些项目。当我得到这个列表时,我想将它与现有的列表进行比较,这样所有旧列表中不存在的新项目都将被添加,而旧列表中不存在的项目将被删除。 例如,这是列表中已存在的项目: [a,b,c] dto中的列表包含以下内容: [b,d] 因此,在这种情况下,将插入[d],并删除[a]和[c] 有一种方法,我可以删除旧列表,然后添加DTO列表中的所有元素,但我不希望以这种方式添加 这就是我所尝试的: public Role updateRoleDTO

我有一个DTO,它包含一个列表,我在DAO中添加或删除了一些项目。当我得到这个列表时,我想将它与现有的列表进行比较,这样所有旧列表中不存在的新项目都将被添加,而旧列表中不存在的项目将被删除。 例如,这是列表中已存在的项目:

[a,b,c]
dto中的列表包含以下内容:

[b,d]
因此,在这种情况下,将插入
[d]
,并删除
[a]
[c]

有一种方法,我可以删除旧列表,然后添加DTO列表中的所有元素,但我不希望以这种方式添加

这就是我所尝试的:

public Role updateRoleDTO(final RoleDTO roleDTO) {
    //...
    //... Some code
    //...
    boolean profilExist = false;
    RoleProfil roleProfil = null;

    // Add non existing profils
    for (Profil profil : roleDTO.getProfils()) {
        profilExist = false;
        roleProfil = new RoleProfil();
        for(Profil oldProfil : oldProfilsList){
            if(profil.getId().equals(oldProfil.getId())){
                profilExist = true;
                break;
            }
        }
        if(!profilExist){
            roleProfil.setRoleId(insertedRole);
            roleProfil.setProfilId(profil);
            roleProfilDAO.insert(roleProfil);
        }
    }

    //Remove existing profils that are not in the updated Role
    for(Profil oldProfil : oldProfilsList){
        profilExist = false;
        for (Profil profil : roleDTO.getProfils()) {
            if(oldProfil.getId().equals(profil.getId())){
                profilExist = true;
                break;
            }
        }
        if(!profilExist){
            roleProfilDAO.delete(roleProfilDAO.findRoleProfilByRoleIdAndProfilId(roleDTO.getRoleId(), oldProfil.getId()));
        }
    }
因此,第一次我将查看旧列表,如果它包含DTO列表中的一个项目,如果它不包含,我将添加它。 第二次,如果DTO的列表中包含旧列表中的项目,我将查看该列表,如果不包含,我将删除它

在这种方法中,我创建了两个循环,每个循环都包含一个实习生循环,看起来太长了


我没有别的办法可以做到吗?或者使用Java 8流,这将使它看起来更好?

如果您可以将数据结构作为一个集合重新建模(并且由于您是通过id进行比较的,似乎您可以通过让Profil的hashCode/equals这样做来实现),那么您可以使用Guava的类轻松地完成:

Set oldSet=Set.newHashSet(“a”、“b”、“c”);
Set newSet=Sets.newHashSet(“b”、“d”);
Sets.SetView toRemove=Sets.difference(oldSet、newSet);
Sets.SetView toInsert=Sets.difference(newSet,oldSet);
Sets.SetView toUpdate=Sets.INTERACTION(旧集、新集);
或者使用Java 8的Streams API:

    Set<String> oldSet = new HashSet<>(Arrays.asList("a", "b", "c"));
    Set<String> newSet = new HashSet<>(Arrays.asList("b", "d"));

    Stream<String> toRemove = oldSet.stream().filter(e -> !newSet.contains(e));
    Stream<String> toInsert = newSet.stream().filter(e -> !oldSet.contains(e));
    Stream<String> toUpdate = oldSet.stream().filter(newSet::contains);
Set oldSet=newhashset(Arrays.asList(“a”、“b”、“c”));
Set newSet=newhashset(Arrays.asList(“b”、“d”));
Stream toRemove=oldSet.Stream().filter(e->!newSet.contains(e));
Stream toInsert=newSet.Stream().filter(e->!oldSet.contains(e));
Stream toUpdate=oldSet.Stream().filter(newSet::contains);
oldprofileslist.addAll(roleDTO.getProfils());
removeIf(op->!roleDTO.getProfils().contain(oldProfile));
oldProfilsList=new ArrayList(新哈希集(oldProfilsList))

oldProfilsList将根据需要提供列表。

(1)避免可变列表。(2) 然后你可以只做
obj.list=newList
我不理解你的例子:“
[a,b,c][b,d]
->
[d]
将被插入,而
[a]
将被删除。”为什么没有删除
c
?@slim obj.list=newList在我的情况下不起作用,因为obj不知道列表,这就是我创建DTO的原因,列表中的项目将插入到另一个对象中。@slim对于你的第二个评论,我已经更新了我的问题,对不起,这是一个错误。我认为你可以先按州对DTO进行分组,例如:
[脏的,新的,删除]
。然后你们就可以互相操作了。
    Set<String> oldSet = new HashSet<>(Arrays.asList("a", "b", "c"));
    Set<String> newSet = new HashSet<>(Arrays.asList("b", "d"));

    Stream<String> toRemove = oldSet.stream().filter(e -> !newSet.contains(e));
    Stream<String> toInsert = newSet.stream().filter(e -> !oldSet.contains(e));
    Stream<String> toUpdate = oldSet.stream().filter(newSet::contains);
oldProfilsList.addAll(roleDTO.getProfils());
   oldProfilsList.removeIf(op ->!roleDTO.getProfils().contain(oldProfile));
   oldProfilsList =   new ArrayList<Profile>(new HashSet<Profile>(oldProfilsList))