File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need help: Best possible result after comparing objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help: Best possible result after comparing objects" Watch "Need help: Best possible result after comparing objects" New topic
Author

Need help: Best possible result after comparing objects

Pupun Moh
Greenhorn

Joined: Apr 14, 2009
Posts: 14
I have a requirement in which a dynamically created bean object has to compared on a ArrayList of similar bean objects to find the best possible match.

Suppose the object which is dynamically created bean object is having values as dynBeanObject{null,null,A,B,C}, where the first two fields have been intentionally set to null.
Now on comparing with the ArrayList of similar objects, results should be an ArrayList of all the objects which have {---,---,A,B,C} as the pattern. Means all the objects which have A,B,C will be added to the new list.

Please guide me to create a filtered array list to which I supply the dynBeanObject and the ArrayList. The return of the method will be an ArrayList of filtered objects.

Regards,
Pupun.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Now on comparing with the ArrayList of similar objects, results should be an ArrayList of all the objects which have {---,---,A,B,C} as the pattern. Means all the objects which have A,B,C will be added to the new list.


Your description pretty much describes what needs to be done, so I am assuming getting started is not the issue. So, what have you tried? and what issues are you running into?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pupun Moh
Greenhorn

Joined: Apr 14, 2009
Posts: 14
Hi Henry,
Thanks for you reply.
Try 1: I tried by individually comparing the values by looping through the entire list, which I realised is not a good solution and also I got stuck in lot of loops with several permutation/combination of possible dynBeanObject.

Try 2:I have an idea of using Comparator, but it will require to write comparator for every field.
But as the bean object is dynamic, so writing a comparator for every filed is not a good solution. There can be several beans...and each bean will have several fields.

Moreover I would like to create a generic solution which can be re-used. Also at runtime, I will neither have idea about the the bean class associated with dynamic bean object and the ArrayList of objects.

Please provide me with some steps to go ahead.

Regards,
Pupun.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Try 1: I tried by individually comparing the values by looping through the entire list, which I realised is not a good solution and also I got stuck in lot of loops with several permutation/combination of possible dynBeanObject.


Why is the solution *not* good? And what permutations are you refering to here? See next point...

Try 2:I have an idea of using Comparator, but it will require to write comparator for every field.
But as the bean object is dynamic, so writing a comparator for every filed is not a good solution. There can be several beans...and each bean will have several fields.


As a side note, you need to write comparing code regardless -- you don't have to do this via comparators though. It could be as simple as overriding the equals() method for each type. But it has to be done, or you can't compare stuff. Now, if you override the equals method (which seems to be all you need), then you can do it in a generic loop -- no permutation of possible types.


Moreover I would like to create a generic solution which can be re-used. Also at runtime, I will neither have idea about the the bean class associated with dynamic bean object and the ArrayList of objects.


Well, that's what the equals() method (and even Comparable and Comparator) is for. It is an interface contract to enable the generic solution that can be reused. With a generic solution, it means that any type can be compared, but it doesn't mean it will be free, if you have a new type that Java doesn't know about, you need to implement the interface.

And think about it? If someone asked you to compare stuff, but doesn't tell you what you are comparing, or how to compare them, how would you do it? Quite frankly, you probably can't. And if you can't do it, how do you expect a program to do it?

Henry
Pupun Moh
Greenhorn

Joined: Apr 14, 2009
Posts: 14
Hi,
Seems things are getting clear.
I will try to implement the solution by overriding equals method. Please correct me if I am wrong. The possible algorithm would be.
1. Pass dynBeanObject and Arraylist to the method.
2. Override equals method to compare every field of the dynBeanObject to the beans from arraylist.
3. Add the mathcing object to the new arrayList and return it.

I have one doubt here, on keeping track of the matching fields. Suppose A matches so I have to keep a track that in the following bean A has matched. Then if B matches, I have to update the the tracking variable.

Something like to keep a record that previous fields have matched and one more filed is left to be matched before adding the object to new arrayList.

Regards,
Pupun
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you have "degrees" of similarity -- i.e. one object can be "more similar" to an object than a second object is -- then an "equals" method isn't going to be of any use. You want a "similarity" method which returns a number between 0 and 1, or between 1 and 100, or between negative infinity and positive infinity, whatever the possibilities are.

Then you need a method which scans the list and picks the member X for which similarity(X, Base) is maximized.
Pupun Moh
Greenhorn

Joined: Apr 14, 2009
Posts: 14
Paul Clapham wrote:If you have "degrees" of similarity -- i.e. one object can be "more similar" to an object than a second object is -- then an "equals" method isn't going to be of any use. You want a "similarity" method which returns a number between 0 and 1, or between 1 and 100, or between negative infinity and positive infinity, whatever the possibilities are.

Then you need a method which scans the list and picks the member X for which similarity(X, Base) is maximized.


Hi Paul,
Thanks for providing a reply and giving more depth to the topic.
If you could please throw some more light and provide a possible solution algorithm. I am trying out possible solutions but always getting stuck at one place or other. It would be nice if you could exemplify your point with respect to my problem.

Hi Henry,
Now as Paul as provided a new dimension to the problem. Please provide your suggestions too.

Regards,
Pupun
Pupun Moh
Greenhorn

Joined: Apr 14, 2009
Posts: 14
As an example to my problem...

Employee Bean:
**************



Class which will have the generic method to compare the DTO object and “ArrayList of DTO objects” for best possible match
***********************************************************************************************




Main class to calling the similarObjects methods:
****************************************



I am not getting any idea as how to go ahead . Please provide me with some algorithm or ideas to work it out.

Regards,
Pupun.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help: Best possible result after comparing objects