Iterate over a HashMap in Java - Four different ways
In this section, we will show you 4 different ways to iterate through a HashMap in Java. Using Advanced for loop Iterating over keys or values using keySet() and values() methods Using Iterator Using Java 8 forEach 1. Using Advanced for loop // create hashmap Map < Integer , String > lanuages = new HashMap<>(); // add 5 different key/values to map lanuages .put( 1 , "Java" ); lanuages .put( 2 , "Scala" ); lanuages .put( 3 , "Kotlin" ); lanuages .put( 4 , "Go" ); lanuages .put( 5 , "Python" ); // iterate via Advanced for loop System . out .println( " \n ==> Advance For Loop Example.." ); for ( Map . Entry < Integer , String > entry : lanuages .entrySet()) { System . out .println( entry ); } 2. Iterating over keys or values using keySet() and values() methods // create hashmap Map < Integer , String > lanuages = new HashMap<>(); // add 5 different key/values to map lanuages .put( 1 , "