Article View: borland.public.jbuilder.java.language
Article #7590WeakReference / Many-Many mapping
From: Owen Green
Date: Tue, 12 Jun 2001 11:47
Date: Tue, 12 Jun 2001 11:47
48 lines
2930 bytes
2930 bytes
Hi,
I'd appreciate anyone's advice on the best way to implement a many-many
mapping; I've got the following sort of interface in mind
public interface CatsHousesMap
public void makeLink(int catID,int houseID);
public void removeLink(int catID, int houseID);
public Collection getCatsHouses(int catID); // Gets all the houses
used by a cat
public Collection getHousesCats(int houseID); // Gets all the cats
that occupy a house
public void removeCat(int catID);
public void removeHouse(int houseID);
}
I had two ideas, both of which seemed to be a bit gritty:
1) Have two hash tables in the impl. - one for houses, one for cats. Each
key maps to a list of values. The makeLink routine just has to make sure
that it updates both tables to ensure consistency, as does removeLink.
However, if the value lists are completly independent of each other, this
makes the removeCat / removeHouse operations very expensive as one would
have to traverse all the lists in the opposite's table looking for
referneces.
So, I thought, why not have a master list, and make everything in the value
lists refer to objects in that? Could something like this be done with
WeakRefernce, where each value list contains a WeakReference to an object
who's strong reference is kept in a master list? Could things be managed
such that the value lists could be made to resize appropriately when a
WeakReference is removed? I've never tangled with the java.lang.ref stuff,
and it seems a tad confusing at present.
2) Abandon the idea of hash tables entirely and make a new sub class of
AbstractList, or something, that expososes two (instead of one)
ListIterators - one for searching by Cats, and the other by Houses. I
haven't really thought this one out yet, but a niggling feeling seems to
suggest that its a no-no.
Any advice (including a proper way of doing it?) gratefully received,
TIA,
Owen Green
--
Software Developer
Unique Interactive Ltd.
London
Technical Details
Message-ID:
<3b26016b_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,acc4b01f466b13be,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-06-12 05:02:35 PST From: "Owen Green" <owen@norobots.unique.com> Newsgroups: borland.public.jbuilder.java.language Subject: WeakReference / Many-Many mapping Date: Tue, 12 Jun 2001 12:47:47 +0100 Lines: 58 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 213.86.7.136 Message-ID: <3b26016b_1@dnews> X-Trace: dnews 992346475 213.86.7.136 (12 Jun 2001 04:47:55 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!dnews