How associate two HashMaps

Discussion in 'Java' started by Ricardo Nuno, May 8, 2012.

  1. Ricardo Nuno

    Ricardo Nuno Guest

    What do I need to associate a value, from one hashMap to a key from a
    new HashMap

    thanks
    Ricardo Nuno, May 8, 2012
    #1
    1. Advertising

  2. Ricardo Nuno

    Stefan Ram Guest

    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.

    »To associate a value to a key« is a little bit vague.

    With the same vagueness, the answer is:
    map.put( k, map_.get( k_ )),
    map.put( map_.get( k_ ), v ),
    map.get( k, map_.get( k_ )),
    map.get( map_.get( k_ ), v ), or something.
    Stefan Ram, May 8, 2012
    #2
    1. Advertising

  3. On 08/05/2012 12:24 AM, 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.
    >
    > »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<K,V> extends AbstractMap<K,V> {
    private Map<K,V> map1, map2;
    private BidiMap<V,K> reversed;

    public BidiMap () {
    map1 = new HashMap<K,V>();
    map2 = new HashMap<K,V>();
    reversed = new BidiMap<K,V>(map2, map1, this);
    }

    private BidiMap (Map<K,V> map1, Map<K,V> map2, BidiMap<V,K> reversed) {
    this.map1 = map1; this.map2 = map2; this.reversed = reversed;
    }

    private BidiMap<V,K> 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.
    javax.swing.JSnarker, May 8, 2012
    #3
  4. Ricardo Nuno

    Roedy Green Guest

    On Mon, 7 May 2012 21:07:56 -0700 (PDT), Ricardo Nuno
    <> wrote, quoted or indirectly quoted someone who said
    :

    >What do I need to associate a value, from one hashMap to a key from a
    >new HashMap


    Think about this first without the generics. You want a HashMap with
    a key and a value. What sort of thing is the key? What is the value?

    see http://mindprod.com/jgloss/hashmap.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Programmers love to create simplified replacements for HTML.
    They forget that the simplest language is the one you
    already know. They also forget that their simple little
    markup language will bit by bit become even more convoluted
    and complicated than HTML because of the unplanned way it grows.
    ..
    Roedy Green, May 8, 2012
    #4
  5. Ricardo Nuno

    Lew Guest

    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.
    >>
    >> »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<K,V> extends AbstractMap<K,V> {


    In Apache Commons Collections 'BidiMap' is an interface with a double handful
    of implementing classes:
    <http://commons.apache.org/collections/api-release/org/apache/commons/collections/BidiMap.html>

    > private Map<K,V> map1, map2;
    > private BidiMap<V,K> reversed;
    >
    > public BidiMap () {
    > map1 = new HashMap<K,V>();
    > map2 = new HashMap<K,V>();
    > reversed = new BidiMap<K,V>(map2, map1, this);
    > }
    >
    > private BidiMap (Map<K,V> map1, Map<K,V> map2, BidiMap<V,K> reversed) {
    > this.map1 = map1; this.map2 = map2; this.reversed = reversed;
    > }
    >
    > private BidiMap<V,K> 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.


    JSnarker's example is a good one for understanding the mechanics of the
    bidirectional map. The Apache Commons libraries are the go-to source for
    most convenience types not in the standard API.

    If you need a different kind of association, well, there are all kinds of ways
    to do that, as Stefan pointed out.

    Another possibility is that you're looking for a way to associate more thanone 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, R>
    {
    L getLeft();
    R getRight();
    }

    public class BasePair<L, R> implements Pair<L, R>
    {
    private final L left;
    private final R right;
    public BasePair(L left, R right)
    {
    this.left = left;
    this.right = right;
    }

    public L getLeft() { return left; }
    public R getRight() { return right; }
    }

    public class MultiAssociator<K, L, R>
    {
    private final Map<K, Pair<L, R>> associator = new HashMap<>();

    public Pair<L, R> put(K key, Pair<L, R> pair)
    {
    if (key == null || pair == null)
    {
    throw new IllegalArgumentException("Argument cannot be null");
    }
    return associator.put(key, pair);
    }

    public Pair<L, R> put(K key, L left, R right)
    {
    return associator.put(key, new Pair<>(left, right));
    }

    public Pair<L, R> put(K key, L left)
    {
    if (key == null)
    {
    throw new IllegalArgumentException("Argument cannot be null");
    }
    Pair<L, R> value = associator.get(key);
    Pair<L, R> repla = value == null? new Pair<>(left, null)
    : new Pair<>(left, value.getRight());
    return associator.put(key, repla);
    }

    public Pair<L, R> put(K key, R right)
    {
    if (key == null)
    {
    throw new IllegalArgumentException("Argument cannot be null");
    }
    Pair<L, R> value = associator.get(key);
    Pair<L, R> repla = value == null? new Pair(null, right)
    : new Pair(value.getLeft(), right);
    return associator.put(key, repla);
    }

    public Pair<L, R> get(K key)
    {
    return associator.get(key);
    }
    }

    --
    Lew
    Lew, May 8, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maps and Hashmaps

    , May 25, 2004, in forum: Java
    Replies:
    2
    Views:
    2,427
    jAnO!
    May 25, 2004
  2. Marilyn Hart

    Help on Hashmaps

    Marilyn Hart, Jan 22, 2005, in forum: Java
    Replies:
    5
    Views:
    601
    Bjorn Abelli
    Jan 23, 2005
  3. Roedy Green

    HashCodes and HashMaps

    Roedy Green, Sep 20, 2005, in forum: Java
    Replies:
    2
    Views:
    470
    Roedy Green
    Sep 20, 2005
  4. iadb
    Replies:
    0
    Views:
    808
  5. Austin Matzko

    How best to associate two DOM elements?

    Austin Matzko, Dec 3, 2009, in forum: Javascript
    Replies:
    3
    Views:
    96
    Thomas 'PointedEars' Lahn
    Dec 4, 2009
Loading...

Share This Page