Since you are iterating over an indexable collection (lists, etc.), I presume that you can then just iterate with the indices of the elements:

IntStream.range(0, params.size())
  .forEach(idx ->
    query.bind(
      idx,
      params.get(idx)
    )
  )
;

The resulting code is similar to iterating a list with the classic i++-style for loop, except with easier parallelizability (assuming, of course, that concurrent read-only access to params is safe).

Answer from srborlongan on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › java › java foreach with index
How to Use Index With forEach in Java | Delft Stack
February 12, 2024 - The primary purpose of using IntStream.range in conjunction with foreach is to iterate over elements while keeping track of their indices. This approach is particularly useful in scenarios where you need to use the index for calculations, ...
Discussions

Is there a way to access an iteration-counter in Java's for-each loop? - Stack Overflow
This still seems to be the clearest solution, even with List and Iterable interfaces. 2013-12-17T17:14:09.05Z+00:00 ... 100% it seems like after java8 anytime I put a forloop into a PR, everyone wants to remind me that there are a zillion alternatives. But this is readible, canonical and not susceptible to the problem in the first place 2022-06-03T20:52:19.44Z+00:00 ... Java 8 introduced the Iterable#forEach... More on stackoverflow.com
🌐 stackoverflow.com
java - How do I get the current index/key in a "for each" loop - Stack Overflow
That's true, but as much as I love ... your own index defeat the purpose of simply using the traditional for-each construct? Using what's built into Java will often be more intuitive / readable (though not always) to the Java developer. 2017-06-11T19:39:55.273Z+00:00 ... 7 years later... ( wow I can't believe it, time flies) , I agree with you @MichaelM 100%. 2017-06-12T18:16:36.92Z+00:00 ... In Java, you can't, as foreach was meant ... More on stackoverflow.com
🌐 stackoverflow.com
foreach - In detail, how does the 'for each' loop work in Java? - Stack Overflow
But suppose if the list starts ... at index 0 and this error is called an off-by-one error. So to avoid this off-by-one error the concept of a foreach loop is used. There may be other advantages too, but this is what I think is the main concept and advantage of using a foreach loop. ... How does this answer the question? "What would the equivalent for loop look like without using the for each syntax?" 2022-04-13T22:07:29.157Z+00:00 ... In Java 8, they introduced ... More on stackoverflow.com
🌐 stackoverflow.com
3 Reasons why You Shouldn’t Replace Your for-loops by Stream.forEach()
Reasons: Premature optimization You have to learn new syntax You have to learn new syntax More on reddit.com
🌐 r/java
72
76
December 8, 2015
🌐
javaspring
javaspring.net › blog › java-foreach-with-index
Java For-Each Loop with Index: A Comprehensive Guide — javaspring.net
In this example, we use a for loop with an index variable i. We start i at 0 and increment it until it reaches the size of the fruits list. On each iteration, we access the element at the current index using the get method. Java 8 introduced the Stream API, which provides a more functional way to iterate over arrays and collections.
🌐
Baeldung
baeldung.com › home › java › java collections › how to access an iteration counter in a for each loop
How to Access an Iteration Counter in a For Each Loop | Baeldung
January 8, 2024 - It could take the Iterable of the source data, over which the for each loop will run, and the BiConsumer for the operation to perform on each item and its index. We can make this generic with the type parameter T: static <T> void forEachWithCounter(Iterable<T> source, BiConsumer<Integer, T> consumer) { int i = 0; for (T item : source) { consumer.accept(i, item); i++; } }
🌐
CodeSpeedy
codespeedy.com › home › java stream foreach with index
Java stream forEach with index - CodeSpeedy
March 13, 2022 - Here arr is the name of the array ... IntStream maps the elements with the associative indices whereas forEach loop print the stream with the respective index....
🌐
Medium
medium.com › javarevisited › i-need-an-index-with-this-list-iteration-method-1e339fd55ed7
I need an index with this List iteration method | by Donald Raab | Javarevisited | Medium
February 10, 2024 - Since Java 8, it is possible to use IntStream.range() to iterate over a set of indices and use List look ups using get(). IntStream.range() can be used an object-oriented version of an indexed for-loop. List<Integer> list = List.of(1, 2, 3, 4, 5); IntStream.range(1, 4) .forEach(index -> System.out.println(list.get(index) + ":" + index)); // Outputs: // 2:1 // 3:2 // 4:3
🌐
How to do in Java
howtodoinjava.com › home › java streams › how to iterate over a stream with indices
How to Iterate Over a Stream With Indices
September 21, 2022 - So, taking that in a sense, we will use the IntStream class to iterate over the numbers from 0 to the length of our array, filter them by the index, and map them with the corresponding Employee objects for the desired indices.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Iterable.html
Iterable (Java SE 17 & JDK 17)
January 20, 2026 - See Java Language Specification: ... void · forEach · (Consumer<? super T> action) Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception....
🌐
Coderanch
coderanch.com › t › 703218 › java › forEach-loop-starting-index
forEach-loop starting at an index (Beginning Java forum at Coderanch)
December 5, 2018 - I trust you know whether you start at index 6 or 7. You can't do that with a for‑each loop.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-foreach-method-in-java
ArrayList forEach() Method in Java - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate the use of forEach() // with an ArrayList of Strings import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Create an ArrayList of Strings ArrayList<String> s = new ArrayList<>(); s.add("Cherry"); s.add("Blueberry"); s.add("Strawberry"); // Use forEach() to print each fruit s.forEach(System.out::println); } }
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - In the code above, we use the index i to determine the previous (i – 1) and next (i + 1) elements. However, this isn’t possible with the forEach() method because it processes elements individually without exposing their index.
🌐
GeeksforGeeks
geeksforgeeks.org › java › for-each-loop-in-java
For-Each Loop in Java - GeeksforGeeks
3 weeks ago - Explanation: The for-each loop allows only forward iteration. Reverse iteration requires a traditional for loop with an index.
🌐
W3Schools
w3schools.com › java › java_foreach_loop.asp
Java For-Each Loop
abs() acos() addExact() asin() atan() atan2() cbrt() ceil() copySign() cos() cosh() decrementExact() exp() expm1() floor() floorDiv() floorMod() getExponent() hypot() IEEEremainder() incrementExact() log() log10() log1p() max() min() multiplyExact() negateExact() nextAfter() nextDown() nextUp() pow() random() rint() round() scalb() signum() sin() sinh() sqrt() subtractExact() tan() tanh() toDegrees() toIntExact() toRadians() ulp() Java Output Methods ... add() addAll() clear() clone() contains ensureCapacity() forEach() get() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() removeIf() replaceAll() retainAll() set() size() sort() spliterator() subList() toArray() trimToSize() Java LinkedList Methods
🌐
Codecademy
codecademy.com › docs › java › arraylist › .foreach()
Java | ArrayList | .forEach() | Codecademy
April 13, 2025 - Yes, for ArrayList, the `.forEach()` method processes elements in the same order they appear in the list, from index 0 to the last index. 5. Can `.forEach()` be used with null elements in the ArrayList?
Top answer
1 of 16
1310
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
    String item = i.next();
    System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

If the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See the Java Language Specification.

for (int i = 0; i < someArray.length; i++) {
    String item = someArray[i];
    System.out.println(item);
}
2 of 16
559

The construct for each is also valid for arrays. e.g.

String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

for (String fruit : fruits) {
    // fruit is an element of the `fruits` array.
}

which is essentially equivalent of

for (int i = 0; i < fruits.length; i++) {
    String fruit = fruits[i];
    // fruit is an element of the `fruits` array.
}

So, overall summary:
[nsayer] The following is the longer form of what is happening:

for(Iterator<String> i = someList.iterator(); i.hasNext(); ) {
  String item = i.next();
  System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for( : ) idiom, since the actual Iterator is merely inferred.

[Denis Bueno]

It's implied by nsayer's answer, but it's worth noting that the OP's for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.

🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
JSTL forEach Index Values Example - Java Code Geeks
May 13, 2025 - By leveraging the varStatus attribute, we were able to access the index of each element in the collection and pass it to a JavaScript function. This approach is useful when we need to dynamically interact with elements on the client side based ...
🌐
Guru99
guru99.com › home › java tutorials › for-each loop in java
For-each loop in Java
November 8, 2024 - The code has reduced significantly. Also, there is no use of the index or rather the counter in the loop. Do ensure that, the data type declared in the foreach loop must match the data type of the arraylist that you are iterating.
🌐
freeCodeCamp
freecodecamp.org › news › enhanced-for-loops-in-java-how-to-use-foreach-loops-on-arrays
Enhanced For Loops in Java – How to Use ForEach Loops on Arrays
February 17, 2023 - After each index is printed out, it moves to the next index. You can also see it this way: "For every number in the even_numbers array, print number)". class ForEachExample { public static void main(String[] args) { int[] even_numbers = { 2, ...