1

If I put a map in session. Then if I remove an object from map. Will this also change the map in session or do I have to put the map in session again?

            Map map = new HashMap();

        map.put("1", "1");
        map.put("2", "2");
        map.put("3", "3");
        map.put("4", "4");

        Session session = getSession();

        session.setAttribute("myMap", map);

        map.remove("1");
Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
user1990525
  • 3,357
  • 4
  • 16
  • 13

4 Answers4

3

Yes it will update map in the session......

 Map map = new HashMap();
        map.put("1", "1");
        map.put("2", "2");
        map.put("3", "3");
        map.put("4", "4");
        session.setAttribute("myMap", map);

        map.remove("1");
        Object mapw = session.getAttribute("myMap");  
        out.println(mapw);

Output

{3=3, 2=2, 4=4}
Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
1

The session keeps a reference to the object that you put in. If you change the content of the map, the map object reference doesn't change. It's still the same map, so the info you have in the session will also change.

Like so:

Map original_map = new HashMap();
session.setAttribute("myMap", original_map);

// Now put something into original_map...
// The _content_ of the map changes

// Later:
Map retrieved_map = session.getAttribute("myMap");

// you'll find that retreived_map == original_map.
// They're the same object, the same Map reference.
// So the retrieved_map contains all that you put into the original_map.
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Kay
  • 527
  • 2
  • 8
  • It is the same map, but the content of the map changes. – Kay Mar 01 '13 at 03:59
  • Yes, because they are the same map. Like this: `Map map2 = map1; map2.put(...);` -> Now map1 also contans the new element, because they are the same map. Content of map changes, but it's the same map. – Kay Mar 01 '13 at 04:01
  • I though it was clear. Added example for why the Object references are the same: Same Map, so changing one will also change the other, since they're the same Map. Don't confuse the reference to an Object with the content of the Object. – Kay Mar 01 '13 at 04:12
  • I re-read the text, my mistake (maybe I'm too tired for today). – Luiggi Mendoza Mar 01 '13 at 04:13
0

Your map still remains in the session. However, could be a better practice to use a WeakHashMap in this case. See the below link

Weak Hash Map Discussion

Community
  • 1
  • 1
psabbate
  • 767
  • 1
  • 5
  • 22
  • How's this related with the exact question? – Luiggi Mendoza Mar 01 '13 at 04:02
  • A weak hash map may actually not work. Imagine this scenario: Servlet 1 puts something into the session via a weak hash map. Servlet 1 is done, objects get garbage collected. Later, Servlet 2 tries to retrieve the map from the session -> The object references in the map, since they were weak, are gone. If you want to actually keep information in the session, an ordinary Map with normal references would keep the data until the session is closed. – Kay Mar 01 '13 at 04:06
  • @LuiggiMendoza, he's asking about removing and putting objects in a map/session. I think that could be usefull for him to know about this Map implementation. Kay you're right, it may be not suitable for all cases but in other cases, your session may prevent some objects to be garbage collected when they should be erased. The comment about WeakHashMap was just a suggestion, not related to the current problem. – psabbate Mar 01 '13 at 04:14
  • Any suggestion should be a comment rather than an answer – Luiggi Mendoza Mar 01 '13 at 04:15
  • @LuiggiMendoza if you check my answer again, you will see that the first line is answering the question. So you want me to answer the question and then create a comment just for my suggestion? come on .. – psabbate Mar 01 '13 at 04:19
  • @psabbate Actually, yes, or at least clearly distinguish between (a) your answer/explanation and (b) other stuff that isn't related. – Dave Newton Mar 06 '13 at 21:44
0

Yes, it will update.

The reason behind this is all objects in Java are passed by reference unless of course the accessor returns a copy of the object.

Jay Q.
  • 4,979
  • 3
  • 33
  • 36