Java Iterate Map Code Example

Snippet 1
  for (Map.Entry entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
} 
Snippet 2
  // Java program to demonstrate iteration over  
// Map using keySet() and values() methods 
  
import java.util.Map; 
import java.util.HashMap; 
  
class IterationDemo  
{ 
    public static void main(String[] arg) 
    { 
        Map gfg = new HashMap(); 
      
        // enter name/url pair 
        gfg.put("GFG", "geeksforgeeks.org"); 
        gfg.put("Practice", "practice.geeksforgeeks.org"); 
        gfg.put("Code", "code.geeksforgeeks.org"); 
        gfg.put("Quiz", "quiz.geeksforgeeks.org"); 
          
        // using keySet() for iteration over keys 
        for (String name : gfg.keySet())  
            System.out.println("key: " + name); 
          
        // using values() for iteration over keys 
        for (String url : gfg.values())  
            System.out.println("value: " + url); 
    } 
} 
 
Snippet 3
  map.forEach((k, v) -> System.out.printf( " %s : %d \n" , k,v) ); 
Snippet 4
  // Java program to demonstrate iteration over  
// Map.entrySet() entries using for-each loop 
  
import java.util.Map; 
import java.util.HashMap; 
  
class IterationDemo  
{ 
    public static void main(String[] arg) 
    { 
        Map gfg = new HashMap(); 
      
        // enter name/url pair 
        gfg.put("GFG", "geeksforgeeks.org"); 
        gfg.put("Practice", "practice.geeksforgeeks.org"); 
        gfg.put("Code", "code.geeksforgeeks.org"); 
        gfg.put("Quiz", "quiz.geeksforgeeks.org"); 
          
        // using for-each loop for iteration over Map.entrySet() 
        for (Map.Entry entry : gfg.entrySet())  
            System.out.println("Key = " + entry.getKey() + 
                             ", Value = " + entry.getValue()); 
    } 
} 
 
Snippet 5
  //traditional way (long)
for(map::iterator it=m.begin(); it!=m.end(); ++it)
	if(it->second)cout 
Snippet 6
  // Java program for traversing 
// through a hashmap using for-each loop 
  
import java.util.*; 
  
class GfG { 
    public static void main(String[] args) 
    { 
  
        // Consider the hashmap containing 
        // student name and their marks 
        HashMap hm =  
                     new HashMap(); 
  
        // Adding mappings to HashMap 
        hm.put("GeeksforGeeks", 54); 
        hm.put("A computer portal", 80); 
        hm.put("For geeks", 82); 
  
        // Printing the HashMap 
        System.out.println("Created hashmap is" + hm); 
  
        // Loop through the hashmap 
        System.out.println("HashMap after adding bonus marks:"); 
  
        // Using for-each loop 
        for (Map.Entry mapElement : hm.entrySet()) { 
            String key = (String)mapElement.getKey(); 
  
            // Add some bonus marks 
            // to all the students and print it 
            int value = ((int)mapElement.getValue() + 10); 
  
            System.out.println(key + " : " + value); 
        } 
    } 
} 
 

Similar Snippets


For Each Javascript Code Example - java

While Loop Continue Js Code Example - java

Java Creat A Folder Code Example - java

Js How To Prevent A Code From Runing Code Example - java

Remove Last Letter From String Java Code Example - java

Create Copy Of Array From Another Array Code Example - java

Find Duplicates In Arraylist Java Code Example - java

Simpledateformat Example Java Code Example - java

Array Fot String Code Example - java

Java Get Random Index From Array Code Example - java

How To Round A Number Javascript Code Example - java

Firestore Find Doc And Set Data Code Example - java

Java How To Get Files In Resources Code Example - java

Different Types Of Writing If Else Statements Jav Code Example - java

How To Take A Image As A Background In Tkinter In Python Code Example - java

Creating Java Main Method Code Example - java

Switch Case Accepts Byte In Java Code Example - java

Find Duplicate And Repeating Number In Array Code Example - java