Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-311

isEqualSet method in SetUtils could rehash the two input sets before it does the containsAll comparison

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 3.2
    • None
    • Set
    • None

    Description

      Hey Guys

      I had an interesting problem recently where I was comparing 2 sets and the equals method was returning false... maybe it might be easier to understand if I explain using pseudo code:

      set1.equals(set2) = false
      set1.get(0).equals(set2.get(0)) = true
      set1.get(1).equals(set2.get(1)) = true
      set1.get(0).hashCode() == set2.get(0).hashCode() = true
      set1.get(1).hashCode() == set2.get(1).hashCode() = true
      

      So, objects in the two sets are equals, the hashCode of the objects in the two sets are equals, however the equals comparison at the set level was not equals.

      After some time, we figured out that the problem was that the object inside the set was getting modified after it was put into the set. So the new hashCode of the object has changed. However the bucket of the set that it's in hasn't changed. When another piece of code calls the contains method, it looks for the equivalent object in a bucket according to the new hashCode. When it doesn't find it, and it returns false.

      I would like to suggest that the SetUtils.isEqualSet method rehashes contents of the sets given in the parameters before it does the containsAll comparison to avoid the problem caused by this scenario.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            michaelq Michael Qiu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment