-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Map methods should return immutable collections #11304
base: main
Are you sure you want to change the base?
Conversation
|
StructLikeSet has an overridden |
@@ -132,12 +133,12 @@ public Set<CharSequence> keySet() { | |||
keySet.add(wrapper.get()); | |||
} | |||
|
|||
return keySet; | |||
return Collections.unmodifiableSet(keySet); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map.keySet
must be a view of the map, so removals from this collection should affect the maps.
The code before the changes was not correct, because it was not a view and it allowed removals, and removals wouldn't affect the map.
The code after the change is less incorrect: the returned Set is still not a view, but it doesn't allow modifications (so all modifications do affect the map).
A better change would be to fix this map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simple way to make CharSequenceMap
obey Map
contract (which is super important!) is to re-use existing implementation. TreeMap
is one that can be used for that.
The whole class can therefore be replaced with:
/**
* A map that uses char sequences as keys and compares them by value.
*
* @param <V> the type of values
*/
public abstract class CharSequenceMap<V> implements Map<CharSequence, V>, Serializable {
public static <V> Map<CharSequence, V> create() {
return new TreeMap<>(CharSequence::compare);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I submitted this as a separate PR: #11308
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally writing a good Map implementation from scratch is super hard.
I did that for Trino once because JDK's IdentityMap violates (violated?) some Map contract aspects, and it was ton of code and even more tests. Using mainstream implementations is really much saner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally writing a good Map implementation from scratch is super hard.
... and sometimes not worth the effort
After all, one can have map-like class without implementing Map interface -- #11704
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the [email protected] list. Thank you for your contributions. |
StructLikeMap
andCharSequenceMap
methodskeySet()
,values()
,entrySet()
return collections which when modified by consumers will change the underlying maps. We should instead return unmodifiable collections like in PartitionMap#keySet()