2 Replies Latest reply on Jun 27, 2013 6:55 AM by SanchitJavaDev

    Comparing dynamic fields of objects using equals and hashCode methods

    SanchitJavaDev

      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).

       

       

          Map<LocationData, List<LocationData>>

       

       

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

       

       

      LocationData class

       

       

          package com.astreait.bulkloader;

         

          public class LocationData {    

              String locId, deptId, jobTitleId, classificationId;

         

              @Override  

              public boolean equals(Object obj) {        

                  LocationData ld = (LocationData)obj;       

                  return this.deptId.equals(ld.deptId) && this.jobTitleId.equals(ld.jobTitleId) && this.classificationId.equals(ld.classificationId) &&

          this.locId.equals(ld.locId);   

              }

         

              @Override  

              public int hashCode() {        

                  return deptId.hashCode() + jobTitleId.hashCode() + classificationId.hashCode() +locId.hashCode();  

              }

          }

       

       

      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.

       

       

       

       

          TableClass class #1

         

          class TableClass{

              List<ColWithData> cwdList;

         

              @Override

              public boolean equals(Object obj) {

                  boolean returnVal = false;

                          // I need to have the logic to be defined such that

                          // all of the dynamic fields can be compared

                  return returnVal;

              }

         

              @Override

              public int hashCode() {

                  int returnVal = 0;

                          // I need to have the logic to be defined such that

                          // all of the dynamic fields can be found for their individual hashCodes

                  return returnVal;

              }

          }

       

       

      ColWithData class #2

       

       

          class ColWithData{

         

              String col; // here the jobTitleId, classificationId, deptId, locId or any other more fields info can come.

              String data; // The corresponding data or value for each jobTitleId, classificationId, deptId, locId or any other more fields.

         

          }

       

       

      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)

       

       

          Map<TableClass, List<TableClass>> finalMap;