Dealing with the HashSet Problem

The HashSet class is guaranteed to act like a "set", in that it can't contain multiple copies of the same thing... but it's not always clear what constitutes "the same thing." By default, HashSet uses "intensional equality": two things are "the same" if they're at the same location in the computer's memory. This is different from the "extensional equality" that we've been using for most of the semester, and that makes more sense for Strings, StringLists, and StringSets: two Strings are "the same" if they contain the same sequence of characters, two StringLists are "the same" if they contain the same sequence of strings, and two StringSets are "the same" if every String that's in one is also in the other.

Consider the following test case:

StringList steveNDeb = new StringList().addAtEnd("Steve").addAtEnd("Deborah");
StringList again = new StringList().addAtEnd("Steve").addAtEnd("Deborah");

HashSet mySet = new HashSet();
t.checkExpect (mySet.add(steveNDeb), true); // should work
t.checkExpect (mySet.add(again), false); // may fail

t.checkExpect (mySet.size(), 1); // may fail
HashSet doesn't recognize that steveNDeb and again are "the same", so it's perfectly happy to add both into the "set".

I know of two ways to fix this:


Last modified:
Stephen Bloch / sbloch@adelphi.edu