Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "javax.swing.JSnarker" Newsgroups: comp.lang.java.programmer Subject: Re: How associate two HashMaps Date: Tue, 08 May 2012 04:30:18 -0400 Organization: media lab? Lines: 46 Message-ID: References: <2d99b203-7059-4ca9-9632-379cd0ae8c85@ee2g2000vbb.googlegroups.com> NNTP-Posting-Host: 6TdlepZ0dShp04FXD1VIxA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: NewsTap/3.5.5 (iPhone/iPod Touch) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:14391 On 08/05/2012 12:24 AM, Stefan Ram wrote: > Ricardo Nuno<9724@bigfoot.com> writes: >> What do I need to associate a value, from one hashMap to a key from a >> new HashMap > > It is hard to parse this sentence with the comma. > > »To associate a value to a key« is a little bit vague. My guess is that he wants a bidirectional map. For that you'd need a custom class delegating to two internal HashMap instances. Something like public class BidiMap extends AbstractMap { private Map map1, map2; private BidiMap reversed; public BidiMap () { map1 = new HashMap(); map2 = new HashMap(); reversed = new BidiMap(map2, map1, this); } private BidiMap (Map map1, Map map2, BidiMap reversed) { this.map1 = map1; this.map2 = map2; this.reversed = reversed; } private BidiMap getReversedMap () { return reversed; } /* Map methods go here. View methods delegate to map1. Modifying methods operate on both maps, but reverse key and value when modifying map2. Also, if a value is already in map2, looks it up in map2 to find the corresponding key and removes that key from map1 before adding the new mappings to both maps. */ } Changes to the reversed map will be reflected in the original and vice versa. -- public final class JSnarker extends JComponent A JSnarker is an NNTP-aware component that asynchronously provides snarky output when the Ego.needsPuncturing() event is fired in cljp.