Add getOptional or equivalent to primitive maps
Hi,
When dealing with a primitive map (e.g., ObjectDoubleMap), it is possible to call getIfAbsent(key, defaultValue) to get a default value when key is missing, but it is not possible to get the value OR know it is absent in one call.
I suppose something like getOptional that would return a OptionalDouble would make sense, though I'm not sure there are optional types for all primitives, but maybe we can introduce them too?
This API will be great to have! But why only for ObjectPrimitiveMap? Do we need something similar for maps in-general to return an Optional?
@nikhilnanivadekar hehe yes. Well, actually MapIterable already has it, so it is only for primitive maps, but yes, all of them. I was too narrow minded well I wrote the description, I've updated it :)
Perfect! I think we should just have getOptional() instead of labeling it with a particular primitive. That way the API is consistent with MapIterable.
The only downside of this is as @victornoel points out there are only Optional<T>, OptionalInt, OptionalLong and OptionalDouble in JDK. I am not sure it would be a good idea to introduce new Optionals for other primitives in EC, especially with the possibility of generic specialization for primitives being an eventual possibility.
Possible Options:
- Limit to
Int,long,doublemap varieties - Widen types for
byte,short,char,float(option forboolean?) - Box with
Optional<T> - Introduce new
Optionaltypes for missing types
Thoughts?
We should just box the ones not supported by JDK