RFR: 8329760: Add indexOf(Predicate<? super E> filter) to java.util.List interface [v11]
xxDark
duke at openjdk.org
Tue Apr 23 13:54:43 UTC 2024
On Fri, 19 Apr 2024 22:20:48 GMT, Evemose <duke at openjdk.org> wrote:
> > I noticed that most (if not all) methods don't ensure non-nullability of `filter` so NPE would only be thrown if the list is not empty.
>
> Yeah, thats true. not sure if it has to throw NPE even if list is emply
Yes, it does. If it shouldn't, then why isn't code in the java.util.List is like this:
default int findIndex(Predicate<? super T> filter) {
ListIterator<T> iterator = listIterator();
if (!iterator.hasNext()) return -1;
Objects.requireNonNull(filter);
int index = 0;
do {
if (filter.test(iterator.next()))
return index;
index++;
} while (iterator.hasNext());
return -1;
}
Also see methods like `removveIf`. All methods should do checks even if they essentially do nothing.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/18639#issuecomment-2067354358
More information about the core-libs-dev
mailing list