Example 1: foreach iterate over an String array:
public class ForEachExampleString {
public static void main(String... args) {
String[] strArray = {"Java","PHP","Python","R","Ruby","Kotlin"};
for (String str:strArray) {
System.out.println(str);
}
}
}
Output:
Java PHP Python R Ruby Kotlin
Example 2: foreach iterate over an int array:
public class ForEachExampleInteger {
public static void main(String... args) {
int[] intArray = {1,2,3,4,5};
for (int intr:intArray) {
System.out.println(intr);
}
}
}
Output:
1 2 3 4 5
Example 3: foreach iterate over an int ArrayList:
public class ForEachExampleArrayList {
public static void main(String... args) {
ArrayList<String> arrayList = new ArrayList&lr;>();
arrayList.add("Java");
arrayList.add("PHP");
arrayList.add("Sharepoint");
arrayList.add("Python");
for (String str:arrayList) {
System.out.println(str);
}
}
}
Output:
Java PHP Sharepoint Python
Example 4: foreach iterate over an int HashMap:
import java.util.HashMap;
import java.util.Map;
public class ForEachExampleHashMap {
public static void main(String... args) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "Java");
map.put("2", "PHP");
map.put("3", "Python");
map.put("4", "Sharepoint");
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("Key: " + key + ", " + "Value: " + value);
}
}
}
Output:
Key: 1, Value: Java Key: 2, Value: PHP Key: 3, Value: Python Key: 4, Value: Sharepoint

Read more: https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Python matplotlib segmentation fault: 11 macOS Big Sur - Python
- Google translate in spreadsheet - Google
- Java JDBC Transition Management using PreparedStatement Examples - Java
- [Solution] Exception in thread main java.util.EmptyStackException - Java
- Run DynamoDB Local on Docker Container - Docker
- Fix - sudo: systemctl: command not found - Ubuntu
- How to upgrade Docker Desktop on Mac - MacOS
- List of Eclipse versions and future releases (2022-06) - Eclipse