简体   繁体   中英

Is comparing serializable object representation a good idea in java?

I need to compare huge, multiple time nested, complex java object. There are no circular references (ufff). Adding "equal" method to each class takes weeks. I got the idea that maybe serializing these objects to array of bytes, and then comparing these arrays can be easier.

Very simplified example:

public class Car implements Serializable{

  String name;
  String brand;

  public Car(String name, String brand) {
    this.name = name;
    this.brand = brand;
  }
}

public class App {

  public static void main(String[] args) {

    Car carA = new Car("Ka", "Ford");
    Car carB = new Car("P85D", "Tesla");
    Car carC = new Car("Ka", "Ford");

    byte[] carAr = SerializationUtils.serialize(carA);
    byte[] carBr = SerializationUtils.serialize(carB);
    byte[] carCr = SerializationUtils.serialize(carC);

    boolean res = Arrays.equals(carAr, carBr);
    System.out.println(res);

    res = Arrays.equals(carAr, carAr);
    System.out.println(res);

    res = Arrays.equals(carAr, carCr);
    System.out.println(res);
  }
}

My initial tests (for much smaller and simpler structures) show that it works. Unfortunately I cannot find any proof that object properties are always serialized in the same order.

UPDATE

These classes are generated by a maven plugin and I do not know yet, how to intercept that process to add equals and hashCode methods.

If you're looking to compare objects for equality, serializing them and then comparing is pointless when you can simply override the class' equals method and compare them by their fields (there will undoubtedly be cases where certain fields of the class should not be taken into account, and your method of serialization may not offer that).

Adding "equal" method to each class takes weeks.

I'm sure this isn't the case. Modern IDEs are able to generate these methods for you, and libraries (such as Guava) offer methods that can compare objects by their fields in a single line of code.

You might want to give Apache's EqualsBuilder a try, most specifically this method (and variances of it); I think in the latest versions (3.7) it is fully recursive, but I think they also have a bug with Strings only solved in the yet-to-be-released v3.8, checking transient fields is also an option of some of the methods.

No, it isn't a good idea.

First, it ignores the content of all non-serializable base classes.

Second, it ignores any transient fields that may be present.

Third, it takes into account differences in referenced objects that you may wish to ignore.

Fourth, it requires that all classes concerned are serializable.

Fifth, it wastes time and space.

It isn't a good idea to post-modify generated code, but you could look into building a set of custom Comparators , which exist outside the class, such that you can use Comparatar.compare(T object1, T object2) to compare them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM