Re: How to check variables for uniqueness ?
Oliver Wong wrote:
"John Ersatznom" <j.ersatz@nowhere.invalid> wrote in message
news:emd9s6$cns$1@aioe.org...
If you want case insensitivity, use e.g.
foo.add(var3.toLowerCase());
This might not actually work, because of the fickleness of certain human
languages.
?
Yeah, I'd essentially wrap the String in a custom class which overrides
equals to call equalsIgnoreCase, and give that to the Set.
What is obviously missing from java.util is an Equalizer:
public interface Equalizer<T> {
public boolean areEqual (T foo, T bar);
public boolean getHash (T foo);
}
and the ability to pass these to collection constructors to use, the way
those that use order comparison can already be handed a custom comparator.
Problems caused by comparators not consitent with an object's equals
method could be avoided by supplying an Equalizer that is consistent
with the comparator, as well as it obviating the need you perceive to
wrap the String class. (Either way, by the way, you need to replace
hashCode() with a case-insensitive version too, or you'll have strings
that compare equal and have different hash codes, at least potentially.
That at least can't happen if you use add(var.toFooCase()) or similar.)