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,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Ways Compare Dates in Java Programming with Examples - Java
- [Fix] Microsoft AADSTS900561: The endpoint only accepts POST requests. Received a GET request. - Microsoft
- Check help on commands while being on sftp> prompt - FTP
- How to enable Wifi Calling on iPhone - iOS
- Java Interview Questions - Set 2: Core Principles - Java
- How to Generate SHA-512 digest in Notepad++ - NotepadPlusPlus
- How to write hello world different languages syntax - HowTos
- Permanently Set or Change $JAVA_HOME on Mac (macOS) - MacOS