Best practice: how to return keyset of a map from a method in java -


my function returns keyset of hashmap member.

from the javadoc:

the set backed map, changes map reflected in set, , vice-versa.

i want avoid behavior.

i want return unmodifiable set, perhaps copy of set.

i not want changes set or map reflected in 1 another.

are there best practices deal such situation?

personally, i'd go google's guava - i want return unmodifiable set, perhaps copy of set. - if map grows, immutableset won't exhibit growth:

immutableset.copyof(map.keyset());

or if want use standard java api - note resulting set grow map grows:

collections.unmodifiableset(map.keyset());

using either of above considered practice given requirements.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -