Dear all,
I have declared following two hashmap.
public static HashMap<Integer,String[]> m1 = new HashMap<Integer,String[]>();
public static HashMap<Integer,String[]> temp = new HashMap<Integer,String[]>();

At starting point of my execution both map will contain key from 1 to 20 and mapped value with it.
Means key value pair are same in both map.
After some processing i modify value in temp map and do operation on it.

for(int r = 0;r<q;r++)
        {
        Set s = temp.keySet();
        Iterator it1 = s.iterator();
        StringBuilder sb = null;
        String[] a1 = null;
        while(it1.hasNext())
        {
         a1 = temp.get((Integer)it1.next());
            System.out.println("\n /////////////////////////////////////////////////////////////////////////");
            if(len[r]>2)
            {
                sb = new StringBuilder(a1[loc[r]]);
                rep = a1[loc[r]].length()-1;
                //System.out.println("rep = "+rep);
                sb.setCharAt(rep,'*');
                //sb.insert(rep, "*");
                a1[loc[r]]=sb.toString();

                //System.out.println("updated temp = "+temp.containsValue(a1)+" updated m1= "+m1.containsValue(a1));
            }
        }
         System.out.println("\n---------------  Map - temp  ------------------------------------------");
for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet())
        {
                Integer key3 = new Integer((Integer)mapEntry2.getKey());

        System.out.print("\nvalue of key3 = "+key3);

        String aa5[] = m1.get(key3);
            if(temp.containsKey(key3))
                temp.put(key3, aa5);
                }
        }

but it will also modify the value in map-m1, As i m not performing any operation on m1.
please tell me where the problem is.
I have also do the debug, but i can't find problem.
i just get that when i put into temp,the array location of m1 will be putted, so in next iteration the value of m1 is also modified.

please tell me what to do, for replacing only value.

Thanks

but it will also modify the value in map-m1,

It sounds like there is a single object that is used as the value in both HashMaps. If you want separate objects with their own values, you need to create them. Copying a reference to an object does not create a new object.

NormR1:
Values inserted in both the map are with different object like
for m1 - count,dataArray[]
for temp - count1, dataArray1[]

so this is not a problem at all.
Problem is at line 32, when i am putting value again in temp from m1, instead of value the location of value of m1 is also replaced.

So in next iteration the value gets changed in m1 also

So how to put only value in temp map instead of value location of m1.

thanks

Problem is at line 32, when i am putting value again in temp from m1, instead of value the location of value of m1 is also replaced.

That is exactly what NormR1 said "there is a single object that is used as the value in both HashMaps [or are objects being used in both HashMaps]." I didn't answer after NormR1 because I thought you would see the cause of the issue from the quote. It seems that you misunderstood the meaning of the reply...

Can you post a small, complete program that compiles, executes and shows the problem?

At line 30 you get a reference to an array into aa5.
At line 32 you put that reference into temp

Now both HashMaps have references to the same array.

If you want to have two arrays, you need to define a second array and copy the contents of the first array. Then you would have two arrays that refer to the same objects.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.