Results 1 to 6 of 6
  1. #1
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default HashMap with multiple keys for single value

    How can I implement a hashmap with multiple keys for a same values. Please provide a code for better understanding

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: HashMap with multiple keys for single value

    Quote Originally Posted by CodeX Pro View Post
    How can I implement a hashmap with multiple keys for a same values. Please provide a code for better understanding
    A HashMap can map different keys to a same value; the values need not be unique.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: HashMap with multiple keys for single value

    Quote Originally Posted by JosAH View Post
    A HashMap can map different keys to a same value; the values need not be unique.

    kind regards,

    Jos

    Sir Will you please elaborate with examples.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: HashMap with multiple keys for single value

    Quote Originally Posted by CodeX Pro View Post
    Sir Will you please elaborate with examples.
    Sure, you can map, say, (K1, V), (K2, V) and (K3, V) in a HashMap (or a SortedMap), so three different keys K1, K2 an K3 map to the same value V; just as you asked.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: HashMap with multiple keys for single value

    Quote Originally Posted by JosAH View Post
    Sure, you can map, say, (K1, V), (K2, V) and (K3, V) in a HashMap (or a SortedMap), so three different keys K1, K2 an K3 map to the same value V; just as you asked.

    kind regards,

    Jos
    This I have done and it works but I want something like the following:-

    Java Code:
    ArrayList a=new ArrayList();
    
    a.add("@");
    a.add("/\\");
    a.add("/-\\");
    
    HashMap map=new HashMap();
    map.put(a,'A');
    This will work fine and I can even store many keys in 'a' , but how will I check against a particular key from the set of keys when we call

    map.get(a.get("@"));

    but we dont have a get method fir array list that returns the string from the array list if it is present.
    Do we need to override arraylist to make a function of our own like get() .

    I want something like this. Please help.

    Thank You,
    Sincerely,
    CodeX Pro

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: HashMap with multiple keys for single value

    Why are you using raw collections? And don't add an array containing all the keys as a key in the hash map. Add each key individually, i.e.

    Java Code:
    for (String key : array)
       map.put(key, 'A');
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Multiple keys pressed, diagonal movement
    By AndroidAppNewbie in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 09:30 AM
  2. Handling input - multiple keys pressed
    By Shadowstrike in forum Advanced Java
    Replies: 3
    Last Post: 12-30-2010, 01:47 PM
  3. Checking for multiple keys pressed
    By MagnusLarsen in forum New To Java
    Replies: 15
    Last Post: 08-22-2010, 11:51 AM
  4. Multiple Java in a single BOX
    By luke_devon in forum New To Java
    Replies: 2
    Last Post: 07-05-2010, 01:59 PM
  5. data structure with multiple keys
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 06-13-2009, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •