Check if element exists in array using Java 8

Renaud is Not Bill Gates

I have a DTO that contains a list where I add or I remove some items, in my DAO when I get this list, I want to compare it with the existing one, so all new items that doesn't exist in the old list will be added, and the items in the old list that doesn't exist in the list in the dto will be deleted. for example, this is the items in the list that already exist :

[a,b,c]

And the list in the dto contains this :

[b,d]

So in this case [d] will be inserted and [a] and [c] will be removed.

There is an approach where I can just deleted the old list and then add all the elements in the DTO's list, but I don't want it in this way.

This is what I tried :

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()));
        }
    }

So the first time I will look in the old list if it contains an item in the DTO's list, if it doesn't I will add it. In the second time I will look in the DTO's list if it contains an item in the old list, if it doesn't I wll remove it.

In this approach I have created two loops, each loop contains an intern loop, which looks too long.

Isn't there any other approach I can do it ? or using the Java 8 stream which will make it look better?

Kunda

If you can re-model your data structure as a Set (and since you are comparing by id it seems you can do so by just making Profil's hashCode/equals do so), you can easily get it done using Guava's Sets class:

    Set<String> oldSet = Sets.newHashSet("a", "b", "c");
    Set<String> newSet = Sets.newHashSet("b", "d");


    Sets.SetView<String> toRemove = Sets.difference(oldSet, newSet);
    Sets.SetView<String> toInsert = Sets.difference(newSet, oldSet);
    Sets.SetView<String> toUpdate = Sets.intersection(oldSet, newSet);

Or using Java 8's 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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if an element in array exists in Java

Check element exists in array

Check element exists in array

How can I check if an element exists in array of objects using lodash

How to check whether an element exists in an array as in Python using "in"

Check if a cookie array element exists

Safe way to check if array element exists?

How to check if array element exists or not in javascript?

Check if one element exists in an array of objects

How to check if the value exists in the element of an array in typescript?

How to check if element exists in array with jq

Underscore.js to check if element in array exists?

Check if element exists in array without linear search

ColdFusion: how to check if array element exists?

Check if an element exists of another array of objects

How to check if element exists in an array without for loop?

VB.net Check if Array Element Exists

Efficient implementation to check: for each element in array check if it exists in another array

How to check if element exists using a lambda expression?

How to check if a element exists in xml using xpath

Check if element exists on page and continue if not using python

How to check if an element exists using if - elif - else

Check if array exists in array using lodash

How to check if the cache is exists using coldFusion 8?

Check if a row exists using MySQL 8 version

Firestore how do I check if an element exists in an array in cloud firestore using javascript

Check if a record exists in an array using PDO and Mysql

Java: Check if exists three or more matches in array

Using IF commands to Check if a button exists in Selenium Java