dimanche 31 juillet 2016

I can not compare elemets by fields in two Lists

I have Entity User

class User{
private Long id;
private String Name;
private List<SomeClass> someClass;
}

And Entity SomeClass

class SomeClass{
    private Long id;
    private String Name;
    }

I have two object User

user1 and user2

user1.getSomeClass().size() == 5;
user2.getSomeClass().size() == 5;

I need to compare all the elements user1.someClass and user2.someClass

but I do not understand how to do it. how to match any element to which refers. I have bad English but I'll try to explain. I create the first user and create some someClasses for him. After that I try to edit this user. Editing is as follows: I create a new user and copies the old data, if the data is not changed, and if any new fields that have changed. I get the new user - user2. It may so happen that I will edit the item3 of someClass of user2. And After that I need compate this two lists and detect change in it.

if (fromField.getType().equals(List.class)) {
                    List listCurrent = (List) fromField.get(current);
                    List listOld = (List) toField.get(old);
                    for (Object o : listCurrent) {
                        for (Object oldItem : listOld) {
                            if (!oldItem.equals(o)) {
                                //I get each elemet of listCurrent and compare with each element of listOld. but how am I to understand that this object from the first list refers to the objects from the second id if the elements are different? equals method will compare each items in oldItem list 
                        }
                    }

I need to understand whether the new element was added or removed, or modified in user2.someClass





Aucun commentaire:

Enregistrer un commentaire