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 6 of 6

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
    SW Missouri
    Posts
    20,194
    Thanks
    49
    Thanked 2,172 Times in 2,145 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,267
    Thanks
    176
    Thanked 821 Times in 764 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,629
    Thanks
    25
    Thanked 322 Times in 300 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.

Similar Threads

  1. Newbie question: Hashmap with composite object key
    By credible58 in forum Java Theory & Questions
    Replies: 0
    Last Post: December 1st, 2012, 12: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, 09:36 PM
  5. check duplicate key and value in hashmap
    By starmandell in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 9th, 2011, 03:25 PM