1

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!

19
  • 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? Commented Apr 29, 2012 at 21:58
  • it would be good if you could just post the loop/replacement code in question Commented Apr 29, 2012 at 21:58
  • 9
    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. Commented Apr 29, 2012 at 21:58
  • 2
    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? Commented Apr 29, 2012 at 22:24
  • 1
    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")). Commented Apr 29, 2012 at 22:38

2 Answers 2

4

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);
    }
}

Testing:

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

I would say: as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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?
0

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.

2 Comments

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!
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.