“Iterate Thrpugh Hasmap” Code-Antworten

Java iteriert durch Hashmap

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

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());
    }
}
Zenon Codes

durch Hashmap durch foreach Loop wiederholen

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("\nValues: ");
    for (String value : languages.values()) {
      System.out.print(value);
      System.out.print(", ");
    }
    
    // iterate through key/value entries
    System.out.print("\nEntries: ");
    for (Entry<Integer, String> entry : languages.entrySet()) {
      System.out.print(entry);
      System.out.print(", ");
    }
  }
}
Astra Lee

Iterate Thrpugh Hasmap

for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
            int a=entry.getKey();
            int b=entry.getValue();
}
Motionless Macaw

Ähnliche Antworten wie “Iterate Thrpugh Hasmap”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen