Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

retainAll operation on Collection
Hi there,

I am using a number of Collections (actually HashSets) to manipulate
database information. At one point, I want to take the intersection of two
different Collections, i.e., retain all items that are common to both
Collections. I use the following construction:

Collection<Value> collection1 = new HashSet<Value>();
Collection<Value> collection2 = new HashSet<Value>();

/* ... code to enter data in collections ... */

collection1.retainAll(collection2)

with the intention of removing from collection1 all elements that are NOT
present in collection2. I have overridden the equals method of the Value
class contained in the collections to return true if two instances represent
the same data even though they are not the same physical instance. From the
documentation of the retainAll method, I get the impression that the equals
method should be called to determine if two element are ... equal ..., but
that never happens. The end result is that collection1 always end up empty
even though there is an (logical) overlap between the two collections. What
am I missing here?

Best regards,
Per



Report this thread to moderator Post Follow-up to this message
Old Post
Per Ericsson
04-21-05 01:58 AM


Re: retainAll operation on Collection
Have you tried also overriding hashCode() in your Value class?

If you override equals(), you should typically also override hashCode()...

"Per Ericsson" <a@b.c> wrote in message
news:fMx9e.135108$dP1.475543@newsc.telia.net...
> Hi there,
>
> I am using a number of Collections (actually HashSets) to manipulate
> database information. At one point, I want to take the intersection of two
> different Collections, i.e., retain all items that are common to both
> Collections. I use the following construction:
>
> Collection<Value> collection1 = new HashSet<Value>();
> Collection<Value> collection2 = new HashSet<Value>();
>
> /* ... code to enter data in collections ... */
>
> collection1.retainAll(collection2)
>
> with the intention of removing from collection1 all elements that are NOT
> present in collection2. I have overridden the equals method of the Value
> class contained in the collections to return true if two instances
> represent the same data even though they are not the same physical
> instance. From the documentation of the retainAll method, I get the
> impression that the equals method should be called to determine if two
> element are ... equal ..., but that never happens. The end result is that
> collection1 always end up empty even though there is an (logical) overlap
> between the two collections. What am I missing here?
>
> Best regards,
> Per
>



Report this thread to moderator Post Follow-up to this message
Old Post
Mark Oldfield
04-21-05 01:58 AM


Re: retainAll operation on Collection
I hadn't done that. When I wrote my own hashCode function for the Value
class, everything works as it should.

Thanks!
Per
"Mark Oldfield" <mark@idontwantspam.co.uk> skrev i meddelandet
news:d46cji$b35$1@news8.svr.pol.co.uk...
> Have you tried also overriding hashCode() in your Value class?
>
> If you override equals(), you should typically also override hashCode()...
>
> "Per Ericsson" <a@b.c> wrote in message
> news:fMx9e.135108$dP1.475543@newsc.telia.net... 
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Per Ericsson
04-26-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:25 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.