• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing dynamic fields of objects using equals and hashCode methods  RSS feed

 
sanchit dua
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To compare the different objects of the same class with their contents like jobTitleId, classificationId, deptId & classificationId was to be done and do some manipulations later using Set and Map. I was able to do that by simply overriding the equals and hashCode methods of Object class and was able to fetch the information (like in the following Map).



The following is the class I used (its been shown to you so that it can be referred for my problem statement):



Problem:

I'm already known to which all fields of this object I need to make the comparison.
i.e I'm bound to use the variables named classificationId, deptId, jobTitleId & locId etc.

Need:
I need to customize this logic such that the fields Names (classificationId, deptId, jobTitleId & locId etc) can be pulled dynamically along with their values. So, as far as my understanding I made use of 2 classes (TableClass and ColWithData) such that the List of ColWithData is there in TableClass object.

I'm thinking what if I override the same two methods `equals() & hashCode();`
such that the same can be achieved.




Please let me know if I'm proceeding in the right direction or I should make some any other approach. If it is ok to use the current approach then what should be performed in the equals and hashCode methods?

Finally I need to make the map as: (Its not the concern how I will make, but can be considered as my desired result from this logic)



 
sanchit dua
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have found the solution and its working for me. Please let me know if there could be the simple or any other way out finding the solution for this problem.

The code snippet is:



And finally made a map as said:


displaying the things as desired.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sanchit,

Either I've not understood what you are trying to do, or the approach you are following is quite problematic.

1) ColWithData has two fields - col and data. The col field will contain a field name of TableClass field and data will contain the actual data. Now, how do you manage to link these two things? I mean, if your code has 50 TableClass object, how would you map which ColWithData object contains data for which TableClass object? Maybe you are maintaining the list of ColWithData inside TableClass.

2) Why to do duplication of ALL data? I mean, if TableClass is having 10 fields, then you'll have 11th field - which is a list of ColWithData, and it will again contain 10 member, right? Basically, you are duplicating all the data just for the sake of hashCode and equals method.

3) What if value of a field in TableClass changes? Are you also going to do similar modification in cwdList? This way, you'll degrade your code performance, plus the code would be difficult to maintain (and note that by duplicating the data, you've already increased the memory footprint of your code).

4) What if a field in TableClass cannot be expressed in String format? e.g. if TableClass contains a DB connection, what would be data field in corresponding ColWithData object?

I understand that you want to handle the case where you may add/remove fields from TableClass and those fields will be added/removed from hashCode and equals method. But the problem I see is that so much code needs to be modified (if you add a field in TableClass, you'll need to add an entry in cwdList; also, you need to keep track of value modifications of that field, and make corresponding changes to cwdList).

Frankly, I would follow the simple approach:
1) Whenever a field is added/removed, make corresponding changes in hashCode/equals method (just like your original code where you were considering 4 fields).

2) Or, obtain types and data of all fields via reflection - this way you'll never have to change your code even if you add/remove the field of TableClass.

Finally, in the end, you need to ask yourself - how many times the format of your TableClass is going to change? Is each and every field of TableClass object necessary to decide equality? And is it worth the trouble?

I hope this helps.
 
sanchit dua
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anayonkar,

Anayonkar Shivalkar wrote:

1) ColWithData has two fields - col and data. The col field will contain a field name of TableClass field and data will contain the actual data. Now, how do you manage to link these two things? I mean, if your code has 50 TableClass object, how would you map which ColWithData object contains data for which TableClass object? Maybe you are maintaining the list of ColWithData inside TableClass.


Yeah the list of ColWithData is being maintained such that it will contain the 2 elements (the variable name and the value).

2) Why to do duplication of ALL data? I mean, if TableClass is having 10 fields, then you'll have 11th field - which is a list of ColWithData, and it will again contain 10 member, right? Basically, you are duplicating all the data just for the sake of hashCode and equals method.


The duplication is required to do the Mathematical operations (mainly calculating summary) over some of the fields like hours or amount if come. I have to separate the unique one and then perform the related summation if their related List of records contains the duplicates otherwise not.

3) What if value of a field in TableClass changes? Are you also going to do similar modification in cwdList? This way, you'll degrade your code performance, plus the code would be difficult to maintain (and note that by duplicating the data, you've already increased the memory footprint of your code).


If we're concerned about this application then during its execution the fields won't change while it may change for another executing instance. There are some predefined operations we need to perform out of them, which will be provided by the user as an input.

4) What if a field in TableClass cannot be expressed in String format? e.g. if TableClass contains a DB connection, what would be data field in corresponding ColWithData object?


The fields will contain the String values or null but not any other objects like connection of a database.


Frankly, I would follow the simple approach:
1) Whenever a field is added/removed, make corresponding changes in hashCode/equals method (just like your original code where you were considering 4 fields).

2) Or, obtain types and data of all fields via reflection - this way you'll never have to change your code even if you add/remove the field of TableClass.


What if I would like to follow the 2nd approach suggested?
Won't it make the code more complex like handling all those variables and injecting their values in equals and hashCode. Like if we make use of javassist library.

Reviewing my approach:
Yeah it seems very complex approach if we need to deal with the varying fields also it will take too much heap space if the data is going to be in thousands or even lacs.

I would really like to know if I should proceed for #2 approach from your side.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!