1

I am trying to hash my object:

protected XMLGregorianCalendar startTime;

value of getStartTime() is 2018-06-21T12:04:10.000Z

The hashCode method at the bottom from XMLGregorianCalendar does not handle milliseconds, so I will never get a unique hashCode even when I use a startTime with different value for milliseconds like 2018-06-21T12:04:10.111Z

Both 2018-06-21T12:04:10.000Z and 2018-06-21T12:04:10.111Z result in the same hashCode unless I change the seconds, minutes, hour, etc etc. I need it to also take into account the milliseconds.

Is there a different hashCode() method that will handle this? How can I accomplish this?

   public int hashCode() {

        // Following two dates compare to EQUALS since in different timezones.
        // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
        //
        // Must ensure both instances generate same hashcode by normalizing
        // this to UTC timezone.
        int timezone = getTimezone();
        if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
            timezone = 0;
        }
        XMLGregorianCalendar gc = this;
        if (timezone != 0) {
            gc = this.normalize();
        }
        return gc.getYear()
                + gc.getMonth()
                + gc.getDay()
                + gc.getHour()
                + gc.getMinute()
                + gc.getSecond(); //Where's milliseconds???
    }
Vadim
  • 4,027
  • 2
  • 10
  • 26
Joey Corkey
  • 475
  • 1
  • 6
  • 19
  • Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values. – Vadim Nov 21 '18 at 16:24
  • @Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then? – Joey Corkey Nov 21 '18 at 16:53
  • 2
    1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often... – Vadim Nov 21 '18 at 17:59
  • 1
    In the source code I found a comment saying that the hash code (for the class concretely implementing the `XMLGregorianCalendar` abstract class) should be consistent with equals. However, `equals` does distinguish different milliseconds values, `hashCode` does not. While this is not strictly against the specs, it does look like a bug in my eyes. – Ole V.V. Nov 26 '18 at 17:17
  • 1
    Possible duplicate of [Are two Java objects with same hashcodes not necessarily equal?](https://stackoverflow.com/questions/5443136/are-two-java-objects-with-same-hashcodes-not-necessarily-equal) – Ole V.V. Nov 27 '18 at 11:13

0 Answers0