This discussion is archived
2 Replies Latest reply: Mar 5, 2013 6:34 AM by 893253 RSS

Conditional Sorting of a List

893253 Explorer
Currently Being Moderated
Hello All,
I have a list of HashMap as below:
List<HashMap<String,String>> lAddressDataList

From this list I am creating a set of objects as below:
Set<AddressData> lAddressDataSortedSet = new TreeSet<AddressData>() ;
for(int row=0;row < lAddressDataList.size() ;row++)
          {

               HashMap<String,String> aRow = lAddressDataList.get(row);

               if(aRow != null && aRow.size() > 0)
               {
                    AddressData aObj = new AddressData();

                    lStreetHouseNo        = aRow.get("STRAS");
                    lCity                     = aRow.get("CITY");
                    lDistrict                  = aRow.get("DISTRICT");
                    lPincode                  = aRow.get("PINCODE");
                    lCountryCode            = aRow.get("COUNTRYCODE");
                    lStartDateString    = aRow.get("STARTDATE");
                    lEndDateString      = aRow.get("ENDDATE");
                    try
                    {
                         lStartDate = dateFormat.parse(lStartDateString);
                         lEndDate = dateFormat.parse(lEndDateString);
                    }
                    catch( ParseException ex)
                    {
                         Session.getLogger().logError("Error in Converting String to Date" + ex);
                    }

                    aObj.setStreetHouseNo(lStreetHouseNo);
                    aObj.setCity(lCity);
                    aObj.setDistrict(lDistrict);
                    aObj.setPincode(lPincode);
                    aObj.setCountryCode(lCountryCode);
                    aObj.setStartDate(lStartDate);
                    aObj.setEndDate(lEndDate);

                    lAddressDataSortedSet.add(aObj);
               }
          }
I am using TreeSet so that I will not have any duplicates also and I could sort it based on compareTo() method defined in AddressData class.
Definition of AddressData class is as follows:
class AddressData implements Comparable<AddressData>
     {
          Date   startDate             = null;
          Date   endDate               = null;
          String streetHouseNo    = null;
          String city                = null;
          String district            = null;
          String pincode                  = null;
          String countryCode            = null;

          public boolean equals(AddressData aObj)
          {
               return ( this.streetHouseNo.equalsIgnoreCase(aObj.getStreetHouseNo())
                         && this.city.equalsIgnoreCase(aObj.getCity())
                         && this.district.equalsIgnoreCase(aObj.getDistrict()) 
                         && this.pincode.equalsIgnoreCase(aObj.getPincode())
                         && this.countryCode.equalsIgnoreCase(aObj.getCountryCode()));
          }

          public int hashCode() {
               final int prime = 31;
               int result = 1;
               result = prime * result + ((streetHouseNo == null) ? 0 : streetHouseNo.hashCode());
               result = prime * result
                         + ((city == null) ? 0 : city.hashCode());
               result = prime * result
                         + ((district == null) ? 0 : district.hashCode());
               result = prime * result
                         + ((pincode == null) ? 0 : pincode.hashCode());
               result = prime * result 
                         + ((countryCode == null) ? 0 : countryCode.hashCode());
               return result;
          }

          public int compareTo(AddressData aObj)
          {
               if( this.streetHouseNo.equalsIgnoreCase(aObj.getStreetHouseNo())
                         && this.city.equalsIgnoreCase(aObj.getCity())
                         && this.district.equalsIgnoreCase(aObj.getDistrict()) 
                         && this.pincode.equalsIgnoreCase(aObj.getPincode())
                         && this.countryCode.equalsIgnoreCase(aObj.getCountryCode()))
               {
                    if(aObj.getStartDate().compareTo(this.startDate) >= 0) 
                          aObj.setStartDate(this.startDate);
                    else
                          aObj.setEndDate(this.endDate);
                    return 0;
               }
               else
                    return aObj.getStartDate().compareTo(this.startDate);
          }

          /*getters and setters */
     }
I created AddressData class so that I could create a SET of objects from List of HashMap and then sort them according to a condition which I wrote in compareTo() method.
I will describe the condition:
For example if a person was at address A in Jan,at A again in Feb-Mar, at B in apr
Then I should sort list/Set such that I have consolidated records only: at A from Jan to Mar and at B in Apr
This part works fine.

But if he was at A in Jan,at B in Feb, at C in Mar and Again at A in Apr *(When he comes at old address again)*
Then I get wrong consolidated records: at A from Jan to Apr,at B in Feb, at C in Mar

I am using TreeSet and created AddressData class solely for sorting in this desired way. I am not sure if this could be done at the List of HashMap level itself. Kindly suggest how do I proceed.

Thanks in advance.

Edited by: J2EE_Life on Mar 5, 2013 3:43 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points