using HashSet to remove dupes doesn't work
HashSet should remove dups if the objects put into the set have an equals method, right?I have a thoroughly unit-tested class (unit tested the equals method) but no dupes are being stripped out...
> HashSet should remove dups if the objects put into
> the set have an equals method, right?
>
> I have a thoroughly unit-tested class (unit tested
> the equals method) but no dupes are being stripped
> out...
It's a hash set. Have you correctly overriden hashCode(), too?
Make sure you override equals() and hashCode()
that was it.
so about generating a good hashcode -- the class has two members (both strings) on which equality is tested. Is something like
String hashThis = getMem1() + getMem2();
return hashThis.hashCode();
an acceptable way to get a hashCode?
Rene, how do you manage to answer so quickly? Seems like you get advanced notice of questions before they are posted (ESP).
> an acceptable way to get a hashCode?
Whatever makes sense for your objects. The only thing about hashcode is that equal objects have to return equal hashcodes. Nobody said differing objects must have differing hashcodes. How far you're willing to walk to avoid collisions that may or may not have a performance impact on your app depends on you.
> that was it.> > so about generating a good hashcode http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
jverda at 2007-7-14 22:37:27 >

> > String hashThis = getMem1() + getMem2();
> return hashThis.hashCode();
>
>
> an acceptable way to get a hashCode?
What ifa.mem1 = "ab";
a.mem2 = "c";
b.mem1 = "a";
b.mem2 = "bc";
Then object a and b have the same hash code.
> Rene, how do you manage to answer so quickly? Seems> like you get advanced notice of questions before they> are posted (ESP).I'm east of you. It's 9:18 p.m. for me already, so I'm ahead in time.
> Then object a and b have the same hash code.Doesn't necessarily matter much. There are probably many Strings with the same hashCode already.
> Then object a and b have the same hash code.You want to try to make hashcodes reasonably unique. The link I provided has a good general purpose algorithm for doing that. It's impossible to completely avoid collisions, however.
jverda at 2007-7-14 22:37:27 >

> tymer> > that wont happen.What won't happen? Multiple strings or other objects with the same hashcode? Yes it will, but it's not a problem if it does.
jverda at 2007-7-14 22:37:27 >

> It's impossible to completely avoid collisions, however.Object.hashcode() does a pretty good job at it. :) The problem is: you'll never have two equal objects.
> I'm east of you. It's 9:18 p.m. for me already, so> I'm ahead in time.Unfair, it's just 2:25pm over here.
> > I'm east of you. It's 9:18 p.m. for me already, so> > I'm ahead in time.> > Unfair, it's just 2:25pm over here.CT?
Do the Sun guys maintaining these forums try to make them unusable? Is that it? Is that why they like to remove useful functionality?Message was edited by: paulcw
> Do the Sun guys maintaining these forums try to make
> them unusable? Is that it? Is that why they like to
> remove useful functionality?
>
> Message was edited by:
> paulcw
Caught by the "pages navigation not at the buttom of the page" feature?
Jverd, after all of your carrying on about that book i finally bought it yesterday. Cant wait till it comes in. It was online all along?
> Jverd, after all of your carrying on about that book> i finally bought it yesterday. Cant wait till it> comes in. It was online all along?No. There's two sample chapters, that's all.
> Jverd, after all of your carrying on about that book
> i finally bought it yesterday. Cant wait till it
> comes in. It was online all along?
Which book? The Bloch one that I quote the hashCode stuff from? I don't believe I've carried on about it. I've never even read it. I just link to that chapter because somebody else pointed it out and I think the hashCode recipe is simple and useful as a general technique.
I think there are a couple of chapters available online, but not the whole thing.
jverda at 2007-7-21 10:36:11 >

Maybe its jcshell then?I didnt mean "carrying on" badly. There is someone whoalways posts very good advice from that book.
I seem to refer to Effective Java in every other post I make.It's a good book and covers issues that a lot of beginners (and intermediates) stumble over.
> Maybe its jcshell then?> I didnt mean "carrying on" badly. No problem. I know what you meant.
jverda at 2007-7-21 10:36:11 >

Maybe its you then Paul. Someone does, haha.
By the way now that JDK 1.5 is out (and provides support for things that Bloch suggested certain idioms or patterns for in EF), the book is due for a second edition! Get cracking Joshua! (Like he reads these forums....)
> By the way now that JDK 1.5 is out (and provides
> support for things that Bloch suggested certain
> idioms or patterns for in EF), the book is due
> for a second edition! Get cracking Joshua! (Like he
> reads these forums....)
Indeed. For example, implementing a polymorphic alternative to Object.clone(). I've been experimenting with the following interface. It's worked well with my implementations but you can't use it with a Collection since Collection doesn't extend it, for example. I've got to think there's a simpler way, but at least parameterized types has made it more tolerable.
interface Copyable<E> {
public E copy();
}