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
🌐
Blogger
javarevisited.blogspot.com › 2015 › 08 › java-8-journey-of-for-loop-in-java.html
Java 8 - Journey of for loop in Java - for() to forEach() Examples
You don't need to keep track of the index, you don't need to call size() method in every step and it was less error-prone than the previous one, but it was still imperative. You are telling the compiler what to do and how to do like traditional for loop. Things change drastically when the functional style of programming was introduced in Java 8 via lambda expression and new Stream API. Now you can loop over your collection without any loop in Java, you just need to use forEach() method of java.util.Stream class, as shown below :
🌐
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.
🌐
Mkyong
mkyong.com › home › java8 › java 8 foreach print with index
Java 8 forEach print with Index - Mkyong.com
February 16, 2020 - package com.mkyong.java8; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class JavaArrayWithIndex { public static void main(String[] args) { String[] names = {"Java", "Node", "JavaScript", "Rust", "Go"}; List<String> collect = IntStream.range(0, names.length) .mapToObj(index -> index + ":" + names[index]) .collect(Collectors.toList()); collect.forEach(System.out::println); } }
🌐
Mkyong
mkyong.com › home › java8 › java 8 foreach examples
Java 8 forEach examples - Mkyong.com
December 4, 2020 - 2.2 Java 8 forEach to loop a List. public static void loopListJava8() { List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); // lambda // list.forEach(x -> System.out.println(x)); // method reference list.forEach(System.out::println); } Output ·
🌐
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 - We can use this with our movie rankings example by providing the implementation for the BiConsumer as a lambda: List rankings = new ArrayList<>(); forEachWithCounter(movies, (i, movie) -> { String ranking = (i + 1) + ": " + movies.get(i); rankings.add(ranking); }); The Java Stream API allows us to express how our data passes through filters and transformations.
🌐
Java Guides
javaguides.net › 2024 › 09 › java-8-foreach-with-index.html
Java 8 – forEach with Index
September 9, 2024 - Step 2: We use AtomicInteger to keep track of the index, starting from 0. The AtomicInteger is used because it is mutable and can be safely incremented inside the lambda expression. Step 3: Inside the forEach() method, we print both the current index and the element by calling index.getAndIncrement(). This increments the index with each iteration. Another way to access the index during iteration is by using IntStream to generate the indexes and forEach() to access the elements. import java.util.Arrays; import java.util.List; import java.util.stream.IntStream; public class ForEachWithIndexUsing
🌐
Oodlestechnologies
oodlestechnologies.com › blogs › java-8-foreach-and-lambda-expression-for-map-and-list
Java 8 forEach and Lambda Expression For Map and List
February 14, 2020 - forEach loop, in Java 8, provides programmers a new, interesting and concise way of iterating over a Collection. Lambda expression is also a new and important feature of Java which was included in Java 8. It provides a concise and clear way to represent one method interface using an expression.
🌐
Medium
neesri.medium.com › master-in-java-8-foreach-48ac3fc940dc
Master in the forEach() Method in Java 8 | by A cup of JAVA coffee with NeeSri | Medium
August 3, 2024 - The forEach() method introduced in Java 8 allows for concise iteration over collections, enhancing code readability and maintainability. It operates on streams and accepts a lambda expression or method reference to perform an action on each element.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › foreach-loop-vs-stream-foreach-vs-parallel-stream-foreach
foreach() loop vs Stream foreach() vs Parallel Stream foreach() - GeeksforGeeks
July 12, 2025 - Lambda operator is not used: foreach loop in Java doesn't use any lambda operations and thus operations can be applied on any value outside of the list that we are using for iteration in the foreach loop.
🌐
Delft Stack
delftstack.com › home › howto › java › java foreach with index
How to Use Index With forEach in Java | Delft Stack
February 12, 2024 - Inside the lambda expression passed to forEach, i represents the current index, and we use it to access and print the corresponding element in the fruits array. This approach maintains the readability and simplicity of a foreach loop while providing ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_foreach.asp
Java ArrayList forEach() Method
import java.util.ArrayList; public ... action on every item in a list. The action can be defined by a lambda expression that is compatible with the accept() method of Java's Consumer interface....
🌐
CodeAhoy
codeahoy.com › java › foreach-in-java
Complete Guide to Java 8 forEach | CodeAhoy
February 19, 2021 - Functional interfaces allow us to use Lambda expressions to write concise code by avoiding object instantiation or anonymous classes (which look even more ugly especially when there’s quite a bit of code.) Recall from the last example (in 2.1) that forEach() method takes a Consumer object which is a Functional Interface.
🌐
Studyeasy
studyeasy.org › course-articles › java-en-en › s13l06-foreach-loop-for-lambda-expression
S13L06 – ForEach loop for Lambda expression – Studyeasy
February 13, 2025 - By the end of this guide, you’ll have a solid understanding of how to leverage ForEach loops with Lambda expressions to write cleaner, more efficient Java code. Iteration over collections is a common task in Java development. Understanding the different ways to iterate helps in writing optimized and readable code. Before Java 8, the primary method for iterating over collections was using the traditional for loop or the enhanced for-each loop. ... Familiar to most developers. Provides access to the index, which can be useful in certain scenarios.
🌐
Javaprogramto
javaprogramto.com › 2020 › 12 › java-foreach-index.html
Java 8 Stream forEach With Index JavaProgramTo.com
So, it is not possible and there are limitations while accessing the variables from inside lambda expressions. Let us explore the ways to get the indices when using forEach method on streams.
🌐
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. We can also collect the Employee instances in a new List if such a requirement exists. IntStream.range(0, employees.length) .filter(i -> i % 2 == 0) .mapToObj(i -> employees[i]) .forEach(System.out::println);
🌐
Baeldung
baeldung.com › home › java › java streams › how to iterate over a stream with indices
How to Iterate Over a Stream With Indices | Baeldung
February 28, 2025 - An alternative approach to tracking indices within a Stream is by utilizing AtomicInteger, which allows us to maintain a mutable counter across lambda expressions. This works well with sequential streams because the elements are processed in order. Here’s how we can implement it: public List<String> getEvenIndexedStringsUsingAtomicInteger(String[] names) { AtomicInteger index = new AtomicInteger(0); return Arrays.stream(names) .filter(name -> index.getAndIncrement() % 2 == 0) .collect(Collectors.toList()); }
🌐
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
🌐
Codecademy
codecademy.com › docs › java › arraylist › .foreach()
Java | ArrayList | .forEach() | Codecademy
April 13, 2025 - This example demonstrates how to print all elements in an ArrayList using the .forEach() method with a lambda expression: