| Author |
Stumped By HashSet
|
Alana Sparx
Ranch Hand
Joined: Feb 14, 2006
Posts: 121
|
|
I have created a utility class with a Hashset that stores public holidays. The idea is you pass a date to the class, together with a number of days you would like added to this date. If the date you pass in falls either on the weekend, or on a public holiday, the utility class will move the passed in date to the next 'business' date. It will then add the number of days you want to increment, and again, if the resulting date falls on a weekend / public holiday, the date is advances to the next 'business' date. I'm using a HashSet to store objects, created in an inner class, where these objects have two properties an Sql Date object a Locale object When I pass a date and locale to the utility class, I create a new instance of the inner class, and use the contains(obj) method of HashSet to check the date. However, contains(obj) insists on returning false! I have iterated through the HashSet and compared the Dates and Locales of my passed in object to those in the collection, and even where the dates and locales match, contains() still insists on returning false. I've set today, tomorrow and Monday as public holidays in the hashset. I want to add one day to today's date as my test - I would therefore expect the next business date to be Tuesday, but the Calendar returns the date as tomorrow! I could get round this by iterating through the HashSet every time and comparing the date & locale, but I'd rather use contains: any idea why it isn't working for me? The code: Test Class Output: Test Date 1: 1148511600000 Test Date 2: 1148598000000 Test Date 3: 1148857200000 Date to be checked: 2006-05-25 Test Date! : 1148511600000 check weekend: 2006-05-25 check publics: 2006-05-25 poss Hol: 2006-05-25 poss Hol: en_GB ######## Dates in HashMap Hol date: 2006-05-26 Hol locl: en_GB Locales match - en_GB Locales match - en_GB Obj not in set: Hol Dates not match - 2006-05-26 Poss Dates not match - 2006-05-25 Hol Locale no match - en_GB Poss Locale no match - en_GB Hol date: 2006-05-29 Hol locl: en_GB Locales match - en_GB Locales match - en_GB Obj not in set: Hol Dates not match - 2006-05-29 Poss Dates not match - 2006-05-25 Hol Locale no match - en_GB Poss Locale no match - en_GB Hol date: 2006-05-25 Hol locl: en_GB Dates match - 2006-05-25 Dates match - 2006-05-25 Locales match - en_GB Locales match - en_GB Obj not in set: Hol Dates not match - 2006-05-25 Poss Dates not match - 2006-05-25 Hol Locale no match - en_GB Poss Locale no match - en_GB ######## check weekend: 2006-05-26 check publics: 2006-05-26 poss Hol: 2006-05-26 poss Hol: en_GB ######## Dates in HashMap Hol date: 2006-05-26 Hol locl: en_GB Dates match - 2006-05-26 Dates match - 2006-05-26 Locales match - en_GB Locales match - en_GB Obj not in set: Hol Dates not match - 2006-05-26 Poss Dates not match - 2006-05-26 Hol Locale no match - en_GB Poss Locale no match - en_GB Hol date: 2006-05-29 Hol locl: en_GB Locales match - en_GB Locales match - en_GB Obj not in set: Hol Dates not match - 2006-05-29 Poss Dates not match - 2006-05-26 Hol Locale no match - en_GB Poss Locale no match - en_GB Hol date: 2006-05-25 Hol locl: en_GB Locales match - en_GB Locales match - en_GB Obj not in set: Hol Dates not match - 2006-05-25 Poss Dates not match - 2006-05-26 Hol Locale no match - en_GB Poss Locale no match - en_GB ######## Date: 2006-05-26 [/code]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
|
I think that you have to override equals() and hashCode() in the HolidayByCountry class. Those are used to retrieve an object from a HashMap
|
[My Blog]
All roads lead to JavaRanch
|
 |
Mandar Max
Ranch Hand
Joined: Mar 14, 2006
Posts: 38
|
|
You should override public boolean equals(Object obj) method of Object class and provide an implementation in your inner class. For the method 'contains(Object o)' to work properly the 'equqls(Object o)' method is necessary. If you look at the description of contains method it says:
boolean contains(Object o) Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
|
"The trouble with doing something right the first time is that nobody appreciates how difficult it was!"
|
 |
Alana Sparx
Ranch Hand
Joined: Feb 14, 2006
Posts: 121
|
|
Thank you for your replies chaps. I've put the following method into the inner class Sadly, it is making no difference. The system out doesn't print so I suspect the method may not be being called. If I really have to override HashCode, what on earth do I do?!? i had a look at the String version - scary!
|
 |
Mandar Max
Ranch Hand
Joined: Mar 14, 2006
Posts: 38
|
|
Forgot to tell you one thing - You should also override hashCode method in your inner class. Equal objects should have equal hashCodes. You can implement a simple hashCode method like this: Now I am sure, your equals() method will be definitely called and you should be able to use the contains() method as you intended. [ May 25, 2006: Message edited by: Mandar Max ]
|
 |
Alana Sparx
Ranch Hand
Joined: Feb 14, 2006
Posts: 121
|
|
Ok, I tried an incredibly complex implementation of hashCode(): This surely cannot be all there is to it? The Calendar is now working though, so I'm happy, but somewhat nervous.......
|
 |
Alana Sparx
Ranch Hand
Joined: Feb 14, 2006
Posts: 121
|
|
Tanks all Mr Max, I took your advice re: implimentation of getHashCode() - just seems to be a tad more robust than my initial pathetic attempt. Many thanks The Spark
|
 |
 |
|
|
subject: Stumped By HashSet
|
|
|