Is it normal to change a value outside the map?

So, I have a piece of code. I go through this question while I was discussing the code with my friend

Map<Integer , List<String>> myMap = new HashMap<Integer , List<String>>(); List<String> list = new ArrayList<String>(); myMap.put(45,list); List<String> lst = myMap.get(45); lst.add("String1"); lst.add("String2"); lst.add("String3"); System.out.println(myMap.get(45)); 

My question is here. -> If its ok to change the list outside the map via another link? I ask in terms of OOP design.

+6
source share
7 answers

This is normal, IMHO

When you write

 List<String> lst = myMap.get(45); 

However, it refers to the value in map , for key 45 .

Once you get the value ( reference in list ), it is up to you what you do with it.

+2
source

If its ok to change the list outside the map via another link? I ask in terms of OOP design.

It depends on the context in which you change it. If you plan on doing this a lot, with many different meanings, then you will quickly find yourself with very confusing code that is difficult to debug and follow.

BUT, in your example, you first download it from the map, and then edit it. It is clear that the data comes from your Map object. If you make this clear with comments and documentation, especially when you pass this link between other methods, this is not a bad practice at all.

+2
source

Everything is in order, provided that you take care of any possible synchronization; for example, if there are several threads that can modify the map and / or list.

You might be confused when you modify a key object. This is clearly NOT normal if the modification violates the hash table invariants; eg.

  • if it causes a key hash change or
  • if it forces the key to give a different result than the other key used in the table.

I ask in terms of OOP design.

I would say that the OO design is neutral on this. You are using a Java interface (i.e. Map ) that does not control values. You do not break encapsulation because the values ​​are not in the Map abstraction.

Whether this is a sound design in terms of application depends on the overall design. We cannot judge one way or another without understanding the context.

+2
source

Each link has a scope, it is your choice (based on your requirement) whether you want the map to be accessed through a multiple link or through a single link.

0
source

Everything is good. After you add numbers to the list in lines 5-7 in the code snippet, and then you get the list from the card again in line 8, the list that you get from the card will have additional numbers that you just added.

0
source

It depends on what you want to do with the list and what your requirements are.

I would say that this is normal, but it would be better to encapsulate it in another object. Consider the question of what to do with empty lists if they are deleted or saved?

Encapsulation allows you to make sure that empty lists are deleted, since then the user will access the wrapper, and not the list directly.

Btw, with HashMap you need to change the list outside the map;)

0
source

ArrayList is mutable. It resizes and retains the same link after modification. To have an immutable list, you must use the following code.

  List<String> list = Collections.unmodifiableList(new ArrayList<String>()); 

If you define a list higher than you cannot change it.

0
source

Source: https://habr.com/ru/post/955010/


All Articles