compare two generic complex objects java

余生长醉 提交于 2019-12-08 03:49:19

问题


My requirement is to compare two objects of the same unknown/generic type. The objects are complex. They can contain Lists which can themselves contain Lists.

So, my initial thoughts are to use a Comparator for comparing objects, reflection for discovering all properties of the bean and some recursion to handle any nested lists the objects may contain.

or, is there a utility that will do all of that for me, in java?


回答1:


I'm currently working on a similar problem. I can confirm that using java reflection you can compare two objects of the same unknown/generic type. I found everything I needed to do so here. Here is a little rundown on how to do it:

  1. Get two objects in a function
  2. Determine all on ob1's fields
  3. Assuming the objects are of equal type you can use the the one field to get the value out of both objects and compare, like so field.get(ob1).equals(field.get(ob2)

However, comparing objects inside of objects is far more difficult. I have no found a solution to it and here is the problem: to compare to items using reflection you need two objects. I have not yet discovered a way to extract an object out of an object of unknown type. Let me know if you find anything.




回答2:


If you can ensure all the nested object tree implements Comparable you could use:

 public int compareTo(Object o) {
   return CompareToBuilder.reflectionCompare(this, o);
 }

http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/builder/CompareToBuilder.html




回答3:


You can use Bean Utils or directly use... Apache Commons EqualsBuilder

a similar solution

EDITED : Please have a look at this post. Upon googling I also found this BeanDiff api which may be useful to you.




回答4:


Ewe. I suppose you could use Reflection to get a list of fields, and go from there. You would need some complicated code to determine if a given field is a simple or complex type, and burrow into the fields that are complex when you are comparing.

See this to get you started on getting a list of fields, their names, and their types




回答5:


I would do it as you already mentioned.

  1. Check if both objects are of the same type and
  2. Iterate over all variables of object 1
    1. Get the value of the variable in object 2
    2. Call valueOfVariableInObject1.compareTo(valueOfVariableInObject2)



回答6:


Disclaimer : I'm not a JAVA dev.

I don't think there's a clean way to compare unknown types. Here's what I would do : declare an interface, let's call it ComparableInterface

interface ComparableInterface {
    function isEqual(ComparableInterface $a);
}

Make all the classes that your unknown object could be an instance of implement this interface. Then use it to compare them.

Any other solutions I can think of would be an ugly hack :/. Also I think if you have to compare thinkg you don't know, you have a problem in your design.



来源:https://stackoverflow.com/questions/10333163/compare-two-generic-complex-objects-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!