Java_Sape
Java_Sape

Reputation: 151

is it ok to change the value from outside a Map?

So i have a code snippet here. I go this issue while i was discussing some 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 here is. -> If its ok to modify the list outside the map through another reference? I am asking from OOP design point of view.

Upvotes: 6

Views: 498

Answers (7)

Stephen C
Stephen C

Reputation: 718946

It is OK, provided that you take care of any potential synchronizations; e.g. if there are multiple threads that might be modifying the map and / or the list.

You might be confusing this with the case where you modify a key object. That is distinctly NOT ok if the modification breaks the hash table invariants; e.g.

  • if it causes either the key's hashcode to change, or
  • if it causes the key to give a different result when compared with some other key used in the table.

I am asking from OOP design point of view.

I'd say that OO design is neutral on this issue. You are using a Java interface (i.e. Map) that doesn't take control of the values. You are not violating encapsulation because the values are not encapsulated by the Map abstraction.

Whether this is sound design from the application perspective depends on the overall design. We can't make a judgement one way or another without understanding the context.

Upvotes: 2

Masudul
Masudul

Reputation: 21971

ArrayList is mutable. It is resizeable and keeps the same reference after modification. To have immutable list you should use following code.

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

If you define list above way, than you can't modify it.

Upvotes: 0

Thomas
Thomas

Reputation: 88707

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

I'd say it is ok-ish but it might be better to encapsulate that in another object. Consider the question what to do with empty lists, should they be removed or kept?

Encapsulation would allow you to ensure that empty lists are removed, since the user would then only access the wrapper, not the list directly.

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

Upvotes: 0

christopher
christopher

Reputation: 27346

If its ok to modify the list outside the map through another reference? I am asking from OOP design point of view.

It really depends on the context in which you're modifying it. If you plan on doing this a lot, with a lot of different values, then you're quickly going to find yourself with very confusing code that is difficult to debug and to follow.

BUT, in your example, you first load it from the map, then you edit it. It's completely clear that the data is coming from your Map object. Provided you make it clear with comments and documentation, especially when you're passing this reference between other methods, this isn't bad practise at all.

Upvotes: 2

NickJ
NickJ

Reputation: 9559

It's OK. After you have added numbers to the list in lines 5-7 in your code snippet, and then you get the list from the map again in line 8, the list you get from the map will have the extra numbers you just added.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

That is completely ok, IMHO

When you write

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

Still it is refering to the value in the map, for the key 45.

Once you get the value(reference to the list), It's up to you what you are doing with it.

Upvotes: 2

Sashi Kant
Sashi Kant

Reputation: 13465

Every reference has a scope, it is your take(based on your requirement) whether you want the Map to be accessed through multiple reference or through a single reference.

Upvotes: 0

Related Questions