On 5/12/21 2:55 AM, Stuart Marks wrote:
As you point out, add() is kind of like addLast(), except without the
reordering semantics for LinkedHashSet. And reversed().add() is a
roundabout way of saying addFirst() -- also without the reordering
semantics for LinkedHashSet. I think most people's reactions would be
"Why didn't they just provide addFirst/addLast?" Plus the reordering
would be missing for LHS.
A second-order issue is performance. I'd expect that implementations
would want to provide a fast-path for addFirst() that is amenable to
JIT optimization; this seems harder to achieve with reversed().add().
The allocation of a reversed view instance typically goes away when C2
compiles the method (if the instance isn't cached like in
AbstractMap.keySet/values) so this can be as performant as specialized
addFirst(), but lack of reordering of existent element in LinkedHashSet
is a different problem I haven thought about.
Regards, Peter