RFR: 8266571: Sequenced Collections
ExE Boss
duke at openjdk.org
Mon Mar 20 23:44:54 UTC 2023
On Sat, 5 Nov 2022 05:44:42 GMT, Stuart Marks <smarks at openjdk.org> wrote:
>> Is there a particular reason we define poll (null on empty) in SequencedMap but remove (NSEE on empty) in SequencedCollection?
>>
>> I understand that SequencedCollection doesn't want to be null-ambiguous, and map entries are non-null so poll there is not ambiguous. But I still think using remove for both look more consistent.
>
> @liach
>
>> Is there a particular reason we define poll (null on empty) in SequencedMap but remove (NSEE on empty) in SequencedCollection?
>>
>> I understand that SequencedCollection doesn't want to be null-ambiguous, and map entries are non-null so poll there is not ambiguous. But I still think using remove for both look more consistent.
>
> Yes, this is definitely an asymmetry. I did it this way to avoid proliferation of new methods, so I just promoted existing ones from NavigableMap into SequencedMap. But I might take another swing at this and see if there's a way to get throwing versions into SequencedMap. The problem is that `firstKey` throws if empty but `firstEntry` returns null if empty. So to make things consistently throwing, we'd need to add `getFirst/LastEntry` and `removeFirst/LastEntry` to SequencedMap (and probably get rid of some of the other methods). This would make SequencedMap and probably LinkedHashMap fairly nice, but it would clutter up NavigableMap.
@stuart-marks
> The problem is that `firstKey` throws if empty but `firstEntry` returns null if empty.
That’s because there’s no way for `firstKey` to distinguish `null` as meaning an `Entry` where `getKey()` returns `null`, and `null` meaning no mapping.
Whereas with `firstEntry`, `null` can only be returned when the map is empty, because a `null` key mapped to some value would return `Entry[key=null, value=…]`.
--------------------------------------------------------------------------------
Another way to think about it is that the default implementation of `firstKey()` does:
default K firstKey() {
return this.firstEntry().getKey();
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/7387#issuecomment-1304500566
More information about the core-libs-dev
mailing list