<html><body><div id="zimbraEditorContainer" style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000" class="23"><div>Hello,</div><div>for me, it seems what you want is Collector on Stream which is able to short-circuit,<br data-mce-bogus="1"></div><div>so you can write<br data-mce-bogus="1"></div><div> list.stream().collect(Collectors.findFirst(s -> s.contains("o")))<br data-mce-bogus="1"></div><div>and in reverse<br data-mce-bogus="1"></div><div> list.reversed().stream().collect(Collectors.findFirst(s -> s.contains("o")))<br data-mce-bogus="1"></div><div> <br data-mce-bogus="1"></div><div>Using a Stream here is more general and will work with other collections like a LinkedHashSet for example.<br data-mce-bogus="1"></div><div>Sadly, there is no way to define a short-circuiting collector :(<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>You can have a short-circuiting Gatherer like this<br data-mce-bogus="1"></div><div> <T> Gatherer<T, ?, Integer> findIndex(Predicate<? super T> predicate) {<br> return Gatherer.ofSequential(<br> () -> new Object() { int index; },<br> Integrtor.ofGreedy((state, element, downstream) -> {<br> var index = state.index++;<br> if (predicate.test(element)) {<br> return downstream.push(index);<br> }<br> return true;<br> }));<br> }<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>and use it like this:<br data-mce-bogus="1"></div><div> list.stream().gather(findIndex(s -> s.contains("o"))).findFirst().orElse(-1);<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>But it's more verbose.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>I wonder if at the same time that the Gatherer API is introduced, the Collector API should be enhanced to support short-circuiting collectors ?<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>regards,<br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"ІП-24 Олександр Ротань" <rotan.olexandr@gmail.com><br><b>To: </b>"core-libs-dev" <core-libs-dev@openjdk.org><br><b>Sent: </b>Friday, April 19, 2024 5:59:39 PM<br><b>Subject: </b>Addition of Predicate-based findIndex and findLastIndex methods to java.util.List<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr">Subject<br>Addition of Predicate-based findIndex and findLastIndex methods to java.util.List<br><br>Motivation<br>The motivation behind this proposal is to enhance the functionality of the List interface by providing a more flexible way to find the index of an element. Currently, the indexOf and lastIndexOf methods only accept an object as a parameter. This limits the flexibility of these methods as they can only find the index of exact object matches.<br><br>Here I want to propose methods that would accept a Predicate as a parameter, allowing users to define a condition that the desired element must meet. This would provide a more flexible and powerful way to find the index of an element in a list.<br><br>The changes I am proposing are implemented in this PR: <a href="https://github.com/openjdk/jdk/pull/18639" target="_blank">https://github.com/openjdk/jdk/pull/18639</a>. Here is a brief overview of the changes made in this pull request:<br><br>Added the findIndex (Predicate<? super E> filter) method to the List interface.<br>Added the findLastIndex (Predicate<? super E> filter) method to the List interface.<br>Implemented these methods in all non-abstract classes that implement the List interface, as well as List itself (default impl).<br>The changes have been thoroughly tested to ensure they work as expected and do not introduce any regressions. The test cases cover a variety of scenarios to ensure the robustness of the implementation.<br><br>For example, consider the following test case:<br><br>List<String> list = new ArrayList<>();<br>list.add("Object one");<br>list.add("NotObject two");<br>list.add("NotObject three");<br><br>int index1 = list.findIndex(s -> s.contains("ct t"));<br>System.out.println(index1); // Expected output: 1<br>int index2 = list. findLastIndex(s -> s.startsWith("NotObject"));<br>System.out.println(index2); // Expected output: 2<br>Currently, to achieve the same result, we would have to use a more verbose approach:<br><br>int index1 = IntStream.range(0, list.size())<br> .filter(i -> list.get(i).contains("ct t"))<br> .findFirst()<br> .orElse(-1);<br>System.out.println(index1); // Output: 1<br>int index2 = IntStream.range(0, list.size())<br> .filter(i -> list.get(i).startsWith("NotObject"))<br> .reduce((first, second) -> second)<br> .orElse(-1);<br>System.out.println(index2); // Output: 2<br><div>Or other approaches that require additional instructions and, therefore, can`t be used in all scopes (like passing argument to method).</div><div><br>I believe these additions would greatly enhance the functionality and flexibility of the List interface, making it more powerful and user-friendly. I look forward to your feedback and am open to making any necessary changes based on your suggestions.<br><br>The main reason I am publishing this proposal in the mailing system is to gather feedback from the Java developers community, especially about possible caveats related to backward compatibility of your projects. Would appreciate every opinion!</div><br><div>Best regards</div></div><br></blockquote></div></div></body></html>