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 ??
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With