• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java Hashset remove duplicates

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking for a solution to my below requirements.

I have a class similar to `ClassA` below

And I have two objects `(obj1, ojb2)` of `ClassA`. The requirement is to merge the two objects and remove the duplicates.

Example:

ClassA Obj1: PlanA = code=AAA, startDate=2010/12/10, endDate=2011/12/10, PlanA =code=BBB, startDate=2010/12/10 endDate=<null>
ClassA obj2: PlanA = code=AAA, startDate=2011/12/10, PlanA= code=CC, startDate=2011/12/10 endDate=<null>, PlanA= code=BBB, startDate=2010/12/10 endDate=2011/12/10,PlanA= code=DDD, startDate=2010/12/10 endDate=2011/12/10

After merging the result should look like:

ClassA obj3: PlanA=code=AAA, startDate=2011/12/10, PlanA= code=CC, startDate=2011/12/10 endDate=<null>,PlanA= code=BBB, startDate=2010/12/10 endDate=<null>,PlanA= code=DDD, startDate=2010/12/10 endDate=2011/12/10

Important Requirement:

When removing the duplicates it should also check if the endDate is present. If the endDate is present then remove that object from the list. For example, the above example has Obj1 PlanA = code=AAA, startDate=2010/12/10, endDate=2011/12/10 and Obj2 PlanA=code=AAA, startDate=2011/12/10 Now the result after merging should have code=AAA, startDate=2011/12/10

I have the below code but it is not creating the result as mentioned in the above important section. Please share with me how to accomplish the result as expected. Thanks for your help




 
Marshal
Posts: 28295
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
Well, the equals() method you have there doesn't match your requirements. But then on the other hand your requirements aren't compatible with an equals() method, because one of them treats the two potentially "equal" thingies (I don't like the term "ClassA" as a description for your object so I'm inventing my own term) asymetrically. Thingies with the same code need to be treated as equal for a while, but then an "equal" thingie with no end date is sort of "more equal" then a thingie with an end date. Let's call that "more important" so as to be less confused.

So I would suggest something like this:

Go through your list of thingies. For each of them see if there's an "equal" thingie in the set. (Your current equals() method is sufficient, I think.) If there is, then see if the current thingie is "more important" than the one already in the set. If it is, then remove the thingie already in the set and replace it by the more important one.

Of course if there isn't an equal in the set, then just put the thingie in the set and carry on.
 
Solomon Gnanadurai
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Paul.
If I understand your reply correctly, are you saying I have to manually use a for loop to go over the set and do the manipulation as per my requirements? Is there any other way to accomplish my requirements? Thanks
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also an error in your hash code method; it appears to use the date to calculate the hash code whereas the date appears not to be used in the equals method. That will breach the general contract of hash code.
 
Solomon Gnanadurai
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did try using just code and lastDay but didn't work as expected.
Do you have any other solution? Appreciate your help.
 
Paul Clapham
Marshal
Posts: 28295
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
Any other solution other than what? Are you sure that your code exactly implemented my suggestion? And if so, what was missing?
 
Solomon Gnanadurai
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't yet tried to use a for loop and remove the duplicates as per my requirements. I was wondering if there is way to use the equals() & hashcode() to get the result that I wanted.
 
Paul Clapham
Marshal
Posts: 28295
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
Not that I can see, and I thought I already explained why I thought that.

So let me take an example. In your set let's suppose you have an element with code ABC and no end date. Then you come across another element with code ABC and end date XYZ. Should this be treated as equal, so that you leave the original element in the set and don't add this one? Or should it be treated as not equal, so that you leave the original element in the set and also add this one? I don't see that either of those alternatives is what you want. And if your requirement is to remove entries from the set (as you said it was), then you're going to have to have some code which explicitly removes entries from the set.
 
Solomon Gnanadurai
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I should not add if the code "ABC" with no end date is already present. But let me take this example. In my set let's suppose I have an element with code "ABC" and with an end date then I come across another element with code "ABC" with no end date then the result should have the code "ABC" with no end date. Basically if the code is same then the result should have the one with no end date.

Example:
ClassA Obj1: PlanA = code=AAA, startDate=2010/12/10, endDate=2011/12/10, PlanA =code=BBB, startDate=2010/12/10 endDate=<null>
ClassA obj2: PlanA = code=AAA, startDate=2011/12/10, PlanA= code=CC, startDate=2011/12/10 endDate=<null>, PlanA= code=BBB, startDate=2010/12/10 endDate=2011/12/10,PlanA= code=DDD, startDate=2010/12/10 endDate=2011/12/10

After merging the result should look like:

ClassA obj3: PlanA=code=AAA, startDate=2011/12/10, PlanA= code=CC, startDate=2011/12/10 endDate=<null>,PlanA= code=BBB, startDate=2010/12/10 endDate=<null>,PlanA= code=DDD, startDate=2010/12/10 endDate=2011/12/10
 
ice is for people that are not already cool. Chill with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic