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,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- MySQL Workbench - Connection Warning - Incompatible/nonstandard server version or connection protocol detected - MySQL
- Android Alert Dialog with Checkboxes example - Android
- How to Exit a Loop in Python Code - Python
- How to install Anaconda on Mac (M1/M2 Mac) - Python
- Bash: Command to Find the Length of a String - Bash
- Python: Print Exception Stack trace like Java - Python
- How choose alternate Tab Bar icon in Notepad++ - NotepadPlusPlus
- Notepad++ Convert text from lower to upper case - NotepadPlusPlus