Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Want to Add Duplicate Key in HashMap

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Want to Add Duplicate Key in HashMap

    Hi,
    I am trying to add duplicate key in HashMap.
    For eg:-HashMap<String,HashMap<String,String>> empMap=new HashMap<String,HashMap<String,String>>();
    I wan to add EmployeeId as key and inner hash map will will DOB and the value.I am trying to store some thing like,
    {101OB:20/10/1985,102OB:18/04/1988}
    But when I try this using hashmap the value of DOB is overwriting
    Any suggestions?

    Thanks in Adv

    Ram


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Want to Add Duplicate Key in HashMap

    If you want more than one value for a key, make the value a list. To add an item, get the value/list and add the new item to the list.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Want to Add Duplicate Key in HashMap

    Quote Originally Posted by rampnq View Post
    Hi,
    I am trying to add duplicate key in HashMap.

    A Map by definition does not allow duplicate keys. I recommend posting an SSCCE, as the posted values - presuming I am reading them correctly - should store fine within the data structure posted if accessed correctly

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Want to Add Duplicate Key in HashMap

    To my understanding of your problem, you have an employee id as the key in your HashMap, and then employee information (which is also stored in a HashMap) in your HashMap's value. Out of curiosity, is there a good reason you are storing the employee information (such as DOB) in a HashMap instead of an object?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Want to Add Duplicate Key in HashMap

    HashMap allows the duplicate keys and override the value with the lastest value of duplicate Key.
    Like Here you are using OB key in inner hashmap so this might be overriding the value of inner class.

    Thanks,

  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Want to Add Duplicate Key in HashMap

    HashMap does not allow duplicate keys.It allows at max one key with null value.

  7. #7

    Default Re: Want to Add Duplicate Key in HashMap

    You can't have duplicate keys in a HashMap. You can, however, create a "multimap" -- a map with Lists as values.

  8. #8
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Want to Add Duplicate Key in HashMap

    Yes. It overwrites it because a map cannot contain duplicate keys. Each key can map up to only one value. Say you're inputing emails, for each email, there is only one value to each key on the hash table. Your most recent email entered is overwritten. Only one value on that map. Understood?

  9. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Want to Add Duplicate Key in HashMap

    The person starting this thread did so 4 years ago. They didn't answer a direct question, nor did they respond to the useful advice they were given at the time. In fact, they though so little of their own thread that they were never heard from again.

    It seems less than pointless to revive a thread that has been long dead - and which did no more than stagger a little and then drop from exhaustion during its brief life. Closing.

Similar Threads

  1. Newbie question: Hashmap with composite object key
    By credible58 in forum Java Theory & Questions
    Replies: 0
    Last Post: December 1st, 2012, 01:21 AM
  2. [SOLVED] Get a new key in HashMap with values of another key.
    By Purple01 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2012, 04:33 AM
  3. retrieve data from database using Hashmap (multiple values in one key)
    By ashin12 in forum Collections and Generics
    Replies: 2
    Last Post: April 4th, 2012, 05:22 AM
  4. Adding key/value pairs from a HashMap in to an ArrayList
    By vb89 in forum Collections and Generics
    Replies: 0
    Last Post: February 26th, 2012, 10:36 PM
  5. check duplicate key and value in hashmap
    By starmandell in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 9th, 2011, 04:25 PM