For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2005 > retainAll operation on Collection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author retainAll operation on Collection
Per Ericsson

2005-04-20, 8:58 pm

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


Mark Oldfield

2005-04-20, 8:58 pm

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
>



Per Ericsson

2005-04-25, 8:58 pm

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...
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com