4

I have a

private Map<String,List<ProductScheme>> discountMap = new HashMap<String,List<ProductScheme>>();

now if i get list from discountMap and add an item in list will i have to put the list again in discount map or it will not be required ??

Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50
  • 1
    Nope, when you do a `get` you get the reference of the list, so what ever changes you do to the list is the same list in the Map – st0le Aug 08 '12 at 10:49
  • Awesome question! I was feeling rusty on my low-level Java and am glad someone worded their question in a way that let me find it. – Shadoninja Jun 27 '16 at 18:37

4 Answers4

12

No it's not required. get returns a reference to the list stored in the map. So whatever modification you do on the list obtained with get (add, remove...) will be reflected on the list in the map too, because they are the same object.

assylias
  • 321,522
  • 82
  • 660
  • 783
9

You only need to add a List if it wasn't there before. A pattern I use is

List<ProductScheme> list = discountMap.get(key);
if (list == null)
    discountMap.put(key, list = new ArrayList<>());
list.add(value);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I would use `if(!discountMap.containsKey(key))`. – nullpotent Aug 08 '12 at 10:53
  • 1
    @iccthedral Using `containsKey` makes sense if you can have a valid `null` value. In this case a `null` list is not valid and you only need to use `get` rather than `containsKey` AND `get` if its there, i.e. doubling the number of lookups. – Peter Lawrey Aug 08 '12 at 10:55
  • Well yes, you're right; but it serves as a safety net. `null` as a value just might happen without you knowing about it. – nullpotent Aug 08 '12 at 10:58
  • 1
    @iccthedral It's not a safety net, it's false security. You're checking for something that isn't what you care about - whether there is a mapping or not is irrelevant. You should be checking for nulls to avoid NPE, using `containsKey` doesn't help that as pointed out above and therefore is only likely to make you ignore the more important null check. Null as a value happening without you knowing about it is exactly why you should check for null, not for a mapping. – Vala Aug 08 '12 at 11:09
  • @iccthedral You can have `map.put(key, null)` in which case the key is in the map but `map.get(key)` returns `null`. You only need to check `containsKey` if you need to know the difference (which is rare) `containsKey` doesn't ensure the value is not `null`. – Peter Lawrey Aug 08 '12 at 11:20
0

since you only get the object-reference to your list from the map, you don't have to put it again to the map.

List someList = discountMap.get("firstList");

is still the same list, just another variable, where you store your pointer to the object.

maxstreifeneder
  • 625
  • 1
  • 8
  • 19
0

NO. You do not need to add modified object again to the discountMap variable. when you call get method from the map it is returning only the reference (object address) of that particular object and you are modifying this object which is there in the map (actually even in the map, it is having the object reference. so same object we are referring from both locations using its memory location) using above object reference.

This is common to all situation where we are referring objects using its reference.

one useful link

Community
  • 1
  • 1