RFR: 8329760: Add indexOf(Predicate<? super E> filter) to java..util.List interface
Evemose
duke at openjdk.org
Thu Apr 18 22:00:14 UTC 2024
On Tue, 9 Apr 2024 20:48:19 GMT, Chen Liang <liach at openjdk.org> wrote:
> A few remarks:
>
> 1. The concern on `indexOf(Object)` and `indexOf(Predicate)` is valid; `Optional` has `orElse` vs. `orElseGet` to avoid these clashes. The behavior change of `indexOf(null)` is a source incompatibility.
> 2. Have you considered `indexOf(predicate/object, startIndex)` like the indexOf for `String`?
>
> Also, a significant difference between String/arrays and `List` is that `List` is structurally modifiable; the index may be no longer valid after a modification. Thus the index is not as useful.
>
> For example, in an [unrolled linked list](https://en.wikipedia.org/wiki/Unrolled_linked_list) implementation, each block can be independently locked for read/write access, maybe like this: `[1, 2, 3] -> [4, 5] -> [6, 7, 8, 9]` An index would be useless if any part of this list updates, but if the position information is stored in a `ListIterator` and we add an `beforeNext(predicate)` that moves the cursor to right before the next element that matches the predicate (or the tail if there's no match), this would be much more useful.
The concern about change in behaviour of indexOf(null) is completely valid, although indexOf(null) is kind of exotic scenario. One possible workaround is to temporally (for an intermidiate period) or permanently delegate null value handing to indexOf(Object), which seems odd for me to me tbh, although it preserves source compatibility.
Regarding to indexOf(predicate/object, startIndex), i thought about this, but firstly I would like to have a final and approved version of indexOf(predicate) to have a reference I`m certain in.
As for beforeNext(Predicate) for ListIterator, I havent thought about that, but now that you`ve mentioned it, I could also consider implementing such thing. However, concern about possible index invalidation is more applicable to the nature of indexOf itself rather then this proposal. This method is primarly used when index is needed right here and now or passed to operation that is not supposed to modify list. I guess ListIterator.beforeNext and List.indexOf serve different, altough alike purpose, and therefore, in my opinion, there are place for both in Java
-------------
PR Comment: https://git.openjdk.org/jdk/pull/18639#issuecomment-2046103546
More information about the core-libs-dev
mailing list