Take the 2-minute tour ×

I have researched this question for ages and cannot get it right!

I have a populated hashmap and an identically formatted hashmap (Map<Integer, ArrayList<String>>) that I have been working on (it has key value/s equal to other keys in the populated hashmap such as 0,1,2 etc). When I use the .put command to update the populated hashmap the few/one I have been working on replaces everything in the populated hashmap - is this normal? Where am I going wrong? I was expecting it to simply replace the key in question + values....

Excuse the not supplying code but it would mean posting quite an amount to demonstrate, just wondering if anyone could help explain where this might be going wrong. I could throw something together to show if needed...

Much obliged!

share|improve this question
I did not understand your question? Do you mean that when you put an already present key in the map why does it get replaced? –  noMAD Apr 29 '12 at 21:58
it would be good if you could just post the loop/replacement code in question –  g13n Apr 29 '12 at 21:58
You will have to post code. Your question is quite incomprehensible and anyway, how could we know what you're doing wrong. Reduce your code to the minimum necessary to reproduce your problem, don't just dump everything you've got. –  Marko Topolnik Apr 29 '12 at 21:58
Ok, there is something fishy going on here. Providing code would possibly solve this fast. Are you sure that the two variables aren't pointing to the same object? That is are you either making a deep-copy of the original hashmap, or when adding elements adding them (with for example put()) to both HashMaps? –  esej Apr 29 '12 at 22:24
The question is unclear. Please give a simple example: What is already in the map? What do you update and how? What would you expect as result? What do you get as result instead? I have the impression your map contains (1, List ("foo", "bar")) - you update (1, "foobar") and expect (1, List ("foo", "bar", "foobar")). –  user unknown Apr 29 '12 at 22:38
show 14 more comments

2 Answers

up vote 2 down vote accepted

This is how a code example might look like:

import java.util.*;

public class NumFormEx
    public static ArrayList <String> listIt (String... params) 
        ArrayList <String> as = new ArrayList <String> ();
        for (String s: params)
            as.add (s);
        return as;

    public static void main (String args[])
        Map <Integer, ArrayList<String>> mils = new HashMap<Integer, ArrayList<String>> ();
        mils.put (1, listIt ("foo", "bar")); 
        mils.put (2, listIt ("zacka", "zacka")); 
        System.out.println ("mils:\t" + mils);
        mils.put (1, listIt ("foobar"));        
        System.out.println ("mils:\t" + mils);


java NumFormEx
mils:   {1=[foo, bar], 2=[zacka, zacka]}
mils:   {1=[foobar], 2=[zacka, zacka]}

I would say: as expected.

share|improve this answer
that does help, thanks! would you expect to be able to take the mils map and .put it into a Map<String, Map<Integer, ArrayList<String>>> to the same effect? –  user1360809 Apr 30 '12 at 0:44
I don't quiet understand to the same effect. Yes, I could put that map into another map, where Strings are keys. Not that easy to imagine a use case, but you could have a mapping of words usages: John used 2 times "zacka zacka", 1 times "foo", 1 times "bar". Peter has a different usage-map. For different names we have different maps, maps get updated, inserted, removed ... . Can you build an example showing problems? –  user unknown Apr 30 '12 at 14:19
add comment

Since map doesn't allow duplicate values you can do :

myMap.put(2, new ArrayList<String>());

This will take element with key 2 and replace it's list with new ("blank") list.

share|improve this answer
I have been using: mainMapLayout.put(int, updateMapLayout); I might have to correct myself as I am adding a Map<Integer, ArrayList<String>> to Map<String, Map<Integer, ArrayList<String>>>. Map<String (for mainMapLayout) = user name and Map<Integer, ArrayList<String>> for customer values (Integer = 0.1,2,3 etc and values = ArrayList<String>) my apologies! –  user1360809 Apr 29 '12 at 22:39
results, original file contents: 2.d.title={0=[1, 2, 3, , 4, 5, 6, 7, 8, , 9, 10, 11], 1=[1, 2, 3, , 4, 5, 6, 7, 8, , 9, 10, 11]}} updated file contents: 2.d.title={0=[1, 2, 3, , 4, 5, 6, 7, 8, , 9, x, 11]}} using command: mainMapLayout.put(string, updateMapLayout); –  user1360809 Apr 29 '12 at 22:43
add comment

Your Answer


