 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()));
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- PHP Code for sending Emails - PHP
- Python Hello World! Program with code example (snippet) - Python
- Java: Convert Stream to List - Java
- Android : Error in http connection java.net.SocketException: Permission denied - Android
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes - Java
- Create SharePoint list from Excel spreadsheet and import table - SharePoint
- 100+ Hashtags for BST ARMY - BTS
- appcompat_v7 errors after updates to API level 21 Material Theme - Android