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,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to add Date and Time to Windows Notepad File - NotepadPlusPlus
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager - Android
- Change label (text) color in tkinter - Python
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement - Java
- How to install and Use Java JDK 21 Initial Release Candidate - Java-JDK-21
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj] - Java
- How to get an embed code from Vimeo? - HowTos
- [Fix] Cannot connect to Microsoft SQL Server, Error: 18456 - Microsoft