Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 7008 invoked by uid 98); 28 Jan 2003 01:58:50 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Received: (qmail 6541 invoked from network); 28 Jan 2003 01:58:47 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 28 Jan 2003 01:58:47 -0000 Received: (qmail 94575 invoked by uid 500); 28 Jan 2003 01:57:14 -0000 Received: (qmail 94559 invoked from network); 28 Jan 2003 01:57:14 -0000 Received: from mailout03.sul.t-online.com (194.25.134.81) by daedalus.apache.org with SMTP; 28 Jan 2003 01:57:14 -0000 Received: from fwd09.sul.t-online.de by mailout03.sul.t-online.com with smtp id 18dKz5-0007Mg-02; Tue, 28 Jan 2003 02:56:19 +0100 Received: from peter (06131234215-0001@[217.86.11.175]) by fwd09.sul.t-online.com with smtp id 18dKyu-0XO4AqC; Tue, 28 Jan 2003 02:56:08 +0100 Message-ID: <000501c2c665$60283700$fe78a8c0@peter> From: Peter_Kossek@t-online.de (=?iso-8859-1?Q?Peter_Ko=DFek?=) To: "Greg Zoller" Cc: "Jakarta Commons Users List" References: <3E356A78.3030109@codaware.com> <003d01c2c634$808d7b30$fe78a8c0@peter> <3E35C718.5010607@codaware.com> Subject: [collections] Question about CollectionUtils semantics Date: Tue, 28 Jan 2003 01:36:51 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 X-Sender: 06131234215-0001@t-dialin.net X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ----- Original Message ----- From: "Greg Zoller" Subject: Re: Question about CollectionUtils semantics > I think the reason your example works is that you're using String-typed > variables... No. That's not really the reason why. > I have another example that uses another object: > > public class Hey { > private String id; > public Hey( String id ) { this.id = id; } > public boolean equals( Object obj ) { > Hey otherHey = (Hey) obj; > if( otherHey.id.equals( this.id ) ) > return true; > return false; > } > public String toString() { return id; } > } Hey - where is your #hashCode implementation? You forgot to fulfill the contract mentioned in http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html#hashCode() . Years before the first Java programmer forgot to fulfill the contract, programmers of other languages did the same: http://wiki.cs.uiuc.edu/VisualWorks/Not+implementing+%23hash+while+overriding+%3 D - and still there's no end in sight. If you didn't know this problem at all, you should have a look into the book "Effective Java" by Joshua Bloch. Incomplete object implementations which override #equals but don't override #hashCode cannot be used within HashSets and HashMaps. The hashing algorithm will fail then. The CollectionUtils#intersection implementation relies on the use of HashMaps. The Collection#retainAll doesn't use hashing, so it can't fail. But if you try to use incomplete object implementations, your code will run into large problems sooner or later, because hashing is used so often. > Regards, > Greg -peter