Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java iterate through hashmap

for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
	System.out.println(entry.getKey() + " = " + entry.getValue());
}
Comment

looping through a hash map and getting the key

 HashMap<Integer, Integer> table = new HashMap<>();
for(Map.Entry<Integer, Integer> key: table.entrySet()){
           System.out.println(key.getKey() + " " + key.getValue());
        }
Comment

java loop hashmap

map.forEach((k, v) -> {
    System.out.format("key: %s, value: %d%n", k, v);
});
Comment

Hashmap iteration

// 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<String,String> gfg = new HashMap<String,String>();
     
        // 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<String,String> entry : gfg.entrySet())
            System.out.println("Key = " + entry.getKey() +
                             ", Value = " + entry.getValue());
    }
}
Comment

iterate through hashMap by forEach loop

import java.util.HashMap;
import java.util.Map.Entry;

class Main {
  public static void main(String[] args) {

    // create a HashMap
    HashMap<Integer, String> languages = new HashMap<>();
    languages.put(1, "Java");
    languages.put(2, "Python");
    languages.put(3, "JavaScript");
    System.out.println("HashMap: " + languages);

    // iterate through keys only
    System.out.print("Keys: ");
    for (Integer key : languages.keySet()) {
      System.out.print(key);
      System.out.print(", ");
    }

    // iterate through values only
    System.out.print("
Values: ");
    for (String value : languages.values()) {
      System.out.print(value);
      System.out.print(", ");
    }
    
    // iterate through key/value entries
    System.out.print("
Entries: ");
    for (Entry<Integer, String> entry : languages.entrySet()) {
      System.out.print(entry);
      System.out.print(", ");
    }
  }
}
Comment

foreach loop java values hashmap

for (V valuesName : arbitraryHashMap.values()) {
	//Do Stuff with valuesName
}
Comment

loop hash map

public static void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        it.remove(); // avoids a ConcurrentModificationException
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: make edittext not editable android 
Java :: java 2d array length 
Java :: variables java 
Java :: programmatically click button javafx 
Java :: java get month 
Java :: how to clear text fields in java 
Java :: java substring after last char 
Java :: hide icon android studio 
Java :: polymorphism in oop 
Java :: how to round double to 2 decimal places java 
Java :: java new char array 
Java :: java inheritance 
Java :: BufferredReader Class 
Java :: android menu change text color 
Java :: install java using cmd 
Java :: pyramid pattern program in java 
Java :: java set textview style 
Java :: register watch service java 
Java :: how to break switch case in java 
Java :: java substring based on delimiter 
Java :: how to subtract localdatetime in java 
Java :: difference between class and interface 
Java :: Conversion from Java List to Array 
Java :: fastreader in java 
Java :: spring log info 
Java :: java char stream to string 
Java :: haxe interface 
Java :: How to efficiently find the start node of a loop within a singly linked list, in Java? 
Java :: swiperefreshlayout 
Java :: remove empty elements from array java 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =