goldmansachs / gs-collections

GS Collections has been migrated to the Eclipse Foundation, re-branded as Eclipse Collections. https://www.eclipse.org/collections/
https://www.eclipse.org/collections/
1.81k stars 276 forks source link

Type-safe `containsKey` `get`, et al #28

Open io7m opened 8 years ago

io7m commented 8 years ago

Hello.

The standard JDK Map<K, V> interfaces contain methods such as containsKey that take an Object instead of a K, throwing away type-safety. I believe this is now considered to be a serious historical mistake and dangerous to the point that some IDEs have added specific checks to warn users when they might be using keys that aren't subtypes of K. I'm wondering if there are any plans to add additional type-safe replacement methods to the MutableMap and ImmutableMap interfaces specified in the GS API? Something along the lines of:

Optional<V> retrieve(K k)
boolean hasKey(K k);

... or:

@Nullable V retrieve(K k)

... if a dependency on Java 8 isn't allowed.

As far as I can tell, the implementations would be trivial and non-intrusive, and would obviously just be defined in terms of the existing unsafe methods.

goldmansachs commented 8 years ago

We currently don't have overloads of containsKey and get but could add them. We have a similar overload of the remove method MutableMapIterable.removeKey(K k): V.

We would probably use the name getKey for the overload of get. We would need to come up with a name for the overload of containsKey, or use your suggestion of hasKey.

io7m commented 8 years ago

I might suggest getValue for the get override, because you're obviously retrieving a value and not a key as getKey would suggest. I've no real preference for any of the names, just getting back type-safety will be enough.