Re: [External] : Re: ReversibleCollection proposal

2021-04-28 Thread Henri Tremblay
Hi,

(I think the first version of this message never went through, so here it
goes just in case)

I just read quickly the proposal and it made me think about another common
issue. I wonder if we could tackle it as well.
I will call it the "find the first element when there is only one" problem.
It happens on unordered collections like a HashSet. It forces to do

Set s = new HashSet<>();
if (s.size() == 1) {
   return s.iterator().next();
   // or
   return s.stream().findFirst().get();
}

Which is a lot of ugliness and object instantiations just to get the first
element.
I would be nice to have a public T findFirst() directly on Iterable.
With a default implementation returning iterator().next(). Things like
ArrayList will want to override will return elementData[0]. It would return
null when the list is empty. Or NoSuchElementException.
It needs to be polished of course but will that be acceptable?

Thanks,
Henri


ReversibleCollection proposal

2021-04-27 Thread Henri Tremblay
Hi,

Not sure if this will end up in the right mailing list thread but let's see.

I just read quickly the proposal and it made me think about another common
issue. I wonder if we could tackle it as well.

I will call it the "find the first element when there is only one" problem.

It happens on unordered collections like a HashSet. It forces to do

Set s = new HashSet<>();

if (s.size() == 1) {
return s.iterator().next();

// or

return s.stream().findFirst().get();
}

Which is a lot of ugliness and object instantiation just to get the first
element.

I would be nice to have a public T findFirst() directly on Iterable.
With a default implementation returning iterator().next(). Things like
ArrayList will want to override will return elementData[0]. It would return
null when the list is empty. Or NoSuchElementException.

It needs to be polished of course but will that be acceptable?

Thanks,
Henri