Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: How associate two HashMaps Date: Tue, 8 May 2012 09:27:58 -0700 (PDT) Organization: http://groups.google.com Lines: 135 Message-ID: <11781872.127.1336494478259.JavaMail.geo-discussion-forums@pbqn10> References: <2d99b203-7059-4ca9-9632-379cd0ae8c85@ee2g2000vbb.googlegroups.com> NNTP-Posting-Host: 69.28.149.29 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1336494851 19303 127.0.0.1 (8 May 2012 16:34:11 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 8 May 2012 16:34:11 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 Xref: csiph.com comp.lang.java.programmer:14417 javax.swing.JSnarker wrote: > Stefan Ram wrote: >> Ricardo Nuno 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. >> >> =BBTo associate a value to a key=AB is a little bit vague. >=20 > My guess is that he wants a bidirectional map. For that you'd need a=20 > custom class delegating to two internal HashMap instances. Something like >=20 > public class BidiMap extends AbstractMap { In Apache Commons Collections 'BidiMap' is an interface with a double handf= ul=20 of implementing classes: > private Map map1, map2; > private BidiMap reversed; >=20 > public BidiMap () { > map1 =3D new HashMap(); > map2 =3D new HashMap(); > reversed =3D new BidiMap(map2, map1, this); > } >=20 > private BidiMap (Map map1, Map map2, BidiMap reversed= ) { > this.map1 =3D map1; this.map2 =3D map2; this.reversed =3D revers= ed; > } >=20 > private BidiMap getReversedMap () { > return reversed; > } >=20 > /* 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. > */ > } >=20 > Changes to the reversed map will be reflected in the original and vice=20 > versa. JSnarker's example is a good one for understanding the mechanics of the=20 bidirectional map. The Apache Commons libraries are the go-to source for=20 most convenience types not in the standard API. If you need a different kind of association, well, there are all kinds of w= ays=20 to do that, as Stefan pointed out. Another possibility is that you're looking for a way to associate more than= one value with the same key. There are a number of ways to do that: Here's one (not tried or even compiled here, so might need tweaking): public interface Pair { L getLeft(); R getRight(); } public class BasePair implements Pair { private final L left; private final R right; public BasePair(L left, R right) { this.left =3D left; this.right =3D right; } public L getLeft() { return left; } public R getRight() { return right; } } public class MultiAssociator { private final Map> associator =3D new HashMap<>(); public Pair put(K key, Pair pair) { if (key =3D=3D null || pair =3D=3D null)=20 { throw new IllegalArgumentException("Argument cannot be null"); } return associator.put(key, pair); } public Pair put(K key, L left, R right) { return associator.put(key, new Pair<>(left, right)); } public Pair put(K key, L left) { if (key =3D=3D null)=20 { throw new IllegalArgumentException("Argument cannot be null"); } Pair value =3D associator.get(key); Pair repla =3D value =3D=3D null? new Pair<>(left, null)=20 : new Pair<>(left, value.getRight()); return associator.put(key, repla); } public Pair put(K key, R right) { if (key =3D=3D null)=20 { throw new IllegalArgumentException("Argument cannot be null"); } Pair value =3D associator.get(key); Pair repla =3D value =3D=3D null? new Pair(null, right)=20 : new Pair(value.getLeft(), right); return associator.put(key, repla); } public Pair get(K key) { return associator.get(key); } } --=20 Lew