Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: HashMap help? Values keep overriding..?

  1. #1
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default HashMap help? Values keep overriding..?

    I create a new hashMap : private HashMap<String, LibraryItem> itemsMap;
    This is to store a string and then a LibraryItem object.

    Constructor: itemsMap = new HashMap<String, LibraryItem>();

    public void storeLibraryItem(LibraryItem li)
    {
    itemsMap.put(new String ("Item"), li);
    }

    It compiles fine, but each of my LibraryItem li objects keep overriding each other until the last one.

    I don't really want to keep adding new .put each time for eg:

    itemsMap.put(new String ("Item"), li);
    itemsMap.put(new String ("Item"), li);
    itemsMap.put(new String ("Item"), li);

    (For each different object because the storeLibraryItem method gets called when I read a file)

    Thanks very much!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: HashMap help? Values keep overriding..?

    This is equivalent to your writing an English-Spanish dictionary that looks like this:

    hello -- hola
    hello -- adiós
    hello -- gracias
    hello -- de nada
    hello -- hoy
    ...

    That wouldn't be a very useful dictionary, and no Spanish speaker would be able to use to help them translate an article in English.

    It's the same for a HashMap -- each key *must* be unique, else it doesn't function. So do that, make each key unique and meaningful, else simply use an ArrayList.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: HashMap help? Values keep overriding..?

    objects keep overriding each other until the last one.
    Another way to look at it. What if you are assigning values to a variable:
    var = val1;
    var = var2;
    var = val3;

    Here the values are overriding the previous ones until the last one.

    If you test what is returned by the put() method you will see if you are replacing an existing value. See the API doc.
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 03-01-2012, 07:51 PM
  2. Cannot return values from hashmap
    By uhertz in forum New To Java
    Replies: 1
    Last Post: 06-17-2011, 04:16 PM
  3. how to get the values from hashmap
    By baktha.thalapathy in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 02:12 PM
  4. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 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
  •