• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help: Best possible result after comparing objects

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Pupun Moh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic