To loop a Map (HashMap) in Java we can follow one of the below ways,
- Using Iterator,
- Using EntrySet,
- Using KeySet,
- Using Java 8 forEach loop,
- Using Java 8 Stream (and Parallel Stream)
Example 1: Using legecay for loop and Iterator
public static void main(String[] args) {
Map<String, String> countryMap = new HashMap<>();
countryMap.put("USA", "Washington DC");
countryMap.put("France", "Paris");
countryMap.put("India", "Delhi");
countryMap.put("Germany", "Berlin");
countryMap.put("China", "Beijing");
countryMap.put("Japan", "Tokyo");
Iterator<Map.Entry<String, String>> mapIterator = countryMap.entrySet().iterator();
for (int i = 1; mapIterator.hasNext(); i++) {
Map.Entry<String, String> element = mapIterator.next();
System.out.println(i + " - " + element.getKey() + " : " + element.getValue());
}
}
Output:
1 - USA : Washington DC
2 - China: Beijing
3 - Japan: Tokyo
4 - France: Paris
5 - Germany: Berlin
6 - India: Delhi
Example 2: Using While loop and Iterator,
Iterator<Map.Entry<String, String>> mapIterator = countryMap.entrySet().iterator();
while (mapIterator.hasNext()) {
Map.Entry<String, String> element = mapIterator.next();
System.out.println(element.getKey() + " : " + element.getValue());
}
Example 3: Using foreach loop and Map.Entry,
for (Map.Entry<String, String> element : countryMap.entrySet()) {
System.out.println(element.getKey() + " : " + element.getValue());
}
Example 4: Using the default foreach method as loop from Map Interface Java 8
countryMap.forEach((country,capitol) ->
System.out.println(country+": "+capitol));
Example 5: Using keySet and foreach loop
for(String key : countryMap.keySet()) {
System.out.println(key +": "+countryMap.get(key));
}
Example 6: Using keySet, Iterator and While loop
Iterator<String> mapIterator = countryMap.keySet().iterator();
while(mapIterator.hasNext()) {
String country = mapIterator.next();
System.out.println(country +": "+countryMap.get(country));
}
Example 7: Using keySet, Iterator and for loop
Iterator<String> mapIterator = countryMap.keySet().iterator();
for(int i = 1;mapIterator.hasNext();i++) {
String country = mapIterator.next();
System.out.println(country +": "+countryMap.get(country));
}
Example 8: Using Java 8 Stream API
countryMap.entrySet().stream().forEach((element) ->
System.out.println(element.getKey()+": "+element.getValue()));
Example 9: Using Java 8 Parallel Stream
countryMap.entrySet().stream().parallel().forEach((element) ->
System.out.println(element.getKey()+": "+element.getValue()));
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:
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode] - Python
- How to know the Version of Notepad App on Windows 11 - Windows-11
- remove div vertical scroll - Html
- [Solution] WslRegisterDistribution failed with error: 0x80370102 VM on Mac - Windows
- How to Pretty Print JSON in PHP - PHP
- Adding Custom ASCII Text Banner in Spring Boot Application - Java
- Python: Convert int to binary String - Python
- SharePoint List redirect user after submitting form NewForm.aspx - SharePoint