 in Java-min.png)
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,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- Open VS Code Command Palette using Keyboard Shortcut - Shortcuts
- Alternatives for Notepad++ on Mac in 2021 - NotepadPlusPlus
- [Java] Generate Getters and Setters in VS Code - Java
- Export aborted because fatal lint error were found - Android
- How to Enable spellcheck Notepad++ - NotepadPlusPlus
- How to adjust MacBook Desktop icons size - MacOS
- Python range() function examples - Python
- What is an Authorization Code Grant? OAuth 2.0 - HowTos