David Y.
—The code below uses a Java for-each loop to iterate through a list:
import java.util.List; import java.util.ArrayList; class Main { public static void main(String[] args) { List<String> products = new ArrayList<>(); products.add("Coffee"); products.add("Tea"); products.add("Chocolate Bar"); for (String product : products) { System.out.println(product); } } }
What does this loop do under the hood and how could I convert my code to do the same thing using a traditional for loop?
The for-each loop was added in Java 5 as syntactic sugar for looping through an Iterator
, such as a List
. The equivalent for loop looks like this:
for (Iterator<String> productIterator = products.iterator(); productIterator.hasNext(); ) { String product = productIterator.next(); System.out.println(product); }
In the for loop expression, we create a new iterator listIterator
and check whether we’ve reached the final iteration with productIterator.hasNext()
. Then, in the body of the for loop, we store and print the value returned by productIterator.next()
. The loop will continue until productIterator.hasNext()
returns false
.
If we used an array instead of a List
, we would need to use the following more traditional syntax instead, as an array is not an iterable:
class Main { public static void main(String[] args) { String[] products = new String[]{"Coffee", "Tea", "Chocolate Bar"}; for (int i = 0; i < products.length; i++) { String product = products[i]; System.out.println(product); } } }
In the above loops:
int i = 0
corresponds to Iterator<String> productIterator = products.iterator();
, as both instantiate the loop.i < products.length
corresponds to productIterator.hasNext()
, as both test whether the loop should continue or finish.i++
is equivalent to productIterator.next()
, as both move to the next item in the list. Even though the loop iterator is conventionally placed inside the loop expression, it can be placed inside the loop block instead with no difference in outcome.Per the Java specification, the for-each loop will be translated to one of these loops at compilation. If the object to be looped through is an iterable (such as a List
), the first loop will be used. If the object to be looped through is an array, the second loop will be used. This allows us to use the same for-each loop syntax for both arrays and iterables.
import java.util.ArrayList; import java.util.List; import java.util.Arrays; class Main { public static void main(String[] args) { List<String> productsList = new ArrayList<>(); productsList.add("Coffee"); productsList.add("Tea"); productsList.add("Chocolate Bar"); String[] productsArray = new String[]{"Coffee", "Tea", "Chocolate Bar"}; for (String product : productsList) { System.out.println(product); } for (String product : productsArray) { System.out.println(product); } // both for loops will produce the same output } }
Note that the iteration variable in a for-each loop merely contains a copy of the value at that point in the list and reassigning it will not change the underlying list. For example:
String[] products = new String[]{"Coffee", "Tea", "Chocolate Bar"}; for (String product : products) { if (product == "Chocolate Bar") { product = "Granola Bar"; } } System.out.println(Arrays.toString(products)); // will print ["Coffee", "Tea", "Chocolate Bar"]
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.