0

I have a class name "Users" and have 2 elements (int)userId and (String)userName.

Let's said

Users obj1 = new Users(10, "User1");
Users obj2 = new Users(11, "User2");

So I want to compare obj1 to obj2 element by element

10 compare to 11, "User1" compare to "User2".

From the research i do from web. It looks like impossible to do it whether convert it to 2d array to compare or whatever method. Is there any method to do this kind of things?

I actually want to do an audit trail so i have the object before changes and after changes, so whatever element that have changed will insert a new record in the audit_trail table with the before value and after value.

I'm a newbie to programming i tried my best to think a way but it just doesn't work. Is there any other way of doing this by SQL? i using ng-admin as (front-end) and API java http to do a update (back-end).

SicaYoumi
  • 176
  • 1
  • 3
  • 20

5 Answers5

0

You need to implement the Comparable<Users> interface. If you want equality check too, then you have to override

boolean equals(Object) 

and

int hashCode()

Read:

https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
and
Why do I need to override the equals and hashCode methods in Java?

Community
  • 1
  • 1
Edd
  • 1,350
  • 11
  • 14
0

From your question, We can compare two different objects. Please implement the equals method to do your operations available in Comparable<Users>.

Let's say as a example below,

Class obj1 = new Class(1, "raja");
Class obj2 = new Class(2, "thiru");

The id and name are a public variable of the class. Then

override the function as,

public boolean equals(Object obj) 
{
    return (this.id == obj.id && this.name.equals(obj.name.equals));
}

Thanks.

Govind Raj
  • 105
  • 6
0

You should override the .equals() method, making your Users class as follows:

public class Users {
    private int mId;
    private String mName;

    public Users(int pId, String pName) {
        mId = pId;
        mName = pName;
    }

    public int getId() {
        return mId;
    }

    @Override
    public boolean equals(Object pObject) {
        return (pObject instanceof Users && ((Users) pObject).getId() == mId);
    }
}
Charlie
  • 2,876
  • 19
  • 26
0

I'd probably create a BeanDelta object

public class PropertyDelta {
    private String propertyName;
    private Object value1;
    private Object value2;

    // constructor & getters
}

public class BeanDelta<T> {
    private Class<T> type;
    private List<PropertyDelta> propertyDeltas = new ArrayList<>();

    public BeanDelta(Class<T> type) {
        this.type = type;
    }

    // getters
}

Then you could write a reflection based method

 public <T> BeanDelta<T> getDelta(T o1, T, o2) {
     Class<T> type = o1.getClass();
     Method[] methods = type.getMethods();
     BeanDelta<T> delta = new BeanDelta<>(type);
     for (Method meth : methods) {
         boolean isGetter = method.getParameterTypes().length == 0 && !method.getReturnType().equals(void.class) && meth.getName().startsWith("get");
         if (isGetter) {
             Object v1 = meth.invoke(o1);
             Object v2 = meth.invoke(o2);

             if (!Objects.equal(v1, v2)) {
                 String propertyName = meth.getName().substring(3);
                 delta.propertyDeltas.add(new PropertyDelta(propertyName, v1, v2));
             }
         }
     }
     return delta;
 }
lance-java
  • 25,497
  • 4
  • 59
  • 101
0

Check it out the solution proposed for do that. http://www.codejava.net/java-core/collections/sorting-a-list-by-multiple-attributes-example

Aditya
  • 41
  • 1
  • 4