Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageHave 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 >
Post Follow-up to this messageI 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... > >
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.