java loop through map
Java
// 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<String, Integer> hm =
new HashMap<String, Integer>();
// 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);
}
}
}
// 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<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 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);
}
}
for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}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
}
}for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
map.forEach((k, v) -> {
System.out.format("key: %s, value: %d%n", k, v);
});
Also in Java:
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- capitalize string java
- Category
- Java
- Title
- java save string in text file
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- jar file not running after being compiled
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- java uuid
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- java hello world
- Category
- Java
- Title
- java get an int from a char
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- java string array to arraylist
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- java create txt file
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- java simple jframe example
- Category
- Java
- Title
- prendere valore da tastiera java
- Category
- Java
- Title
- create java windows application
- Category
- Java
- Title
- comparable on a generic class java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- @JsonSerializer spring specific field string to date
- Category
- Java
- Title
- spigot chat color with symbol
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- esponente in java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- eliminar el primer caracter de un string java
- Category
- Java
- Title
- java 8 group a collections by 2 property
- Category
- Java
- Title
- official java website
- Category
- Java
- Title
- java sort method
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- java random number generator in range
- Category
- Java
- Title
- java call another constructor
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- java script to detect the crome browser
- Category
- Java
- Title
- how to bubblesort a string array in java
- Category
- Java
- Title
- try block in java
- Category
- Java
- Title
- computeifabsent hashmap java
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- java processbuilder example
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- java get folder content
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- how to add strings together
- Category
- Java
- Title
- decomposer chaine de caractère java
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- detect tv remote keys andoid studio
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- java unit test an api
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- linux change java
- Category
- Java
- Title
- java get environment variables
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- get first character of string java
- Category
- Java
- Title
- java
- Category
- Java
- Title
- compare string integer java
- Category
- Java
- Title
- android java convert double to 2 decimal places
- Category
- Java
- Title
- java fileinputstream
- Category
- Java
- Title
- java loop through array
- Category
- Java
- Title
- fusion vecteur ordonner java
- Category
- Java
- Title
- java insert new line in string
- Category
- Java
- Title
- java queue
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- java arraylist add to top
- Category
- Java
- Title
- math maximum java
- Category
- Java
- Title
- leap year program in java
- Category
- Java
- Title
- how to change actionbar color in android programmatically
- Category
- Java
- Title
- how to print to the console in java
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- take string from keyboard java
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- teimpo en segundos java
- Category
- Java
- Title
- java concatenate arrays
- Category
- Java