iterate through hashmap java
Java
for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}// 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 traverse through
// a hashmap using iterator
import java.util.*;
class GfG {
public static void main(String[] args)
{
// Consider the hashmap contains
// 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);
// Getting an iterator
Iterator hmIterator = hm.entrySet().iterator();
// Iterate through the hashmap
// and add some bonus marks for every student
System.out.println("HashMap after adding bonus marks:");
while (hmIterator.hasNext()) {
Map.Entry mapElement = (Map.Entry)hmIterator.next();
int marks = ((int)mapElement.getValue() + 10);
System.out.println(mapElement.getKey() + " : " + marks);
}
}
}
map.forEach((k, v) -> {
System.out.format("key: %s, value: %d%n", k, v);
});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
}
}Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Also in Java:
- Title
- scan a string using scanner class in java
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- java generic calling clone method
- Category
- Java
- Title
- how to set a windows background image tkinter
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- converting char array to string
- Category
- Java
- Title
- java get annotation value
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- how to find substring in java
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- java polymorphism
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- prime factorization java
- Category
- Java
- Title
- java get icon for file type
- Category
- Java
- Title
- transformer un string en Biginteger java
- Category
- Java
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- append to arraylist by index java
- Category
- Java
- Title
- http client java
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- java get environment variables
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- how to have a only number type in java
- Category
- Java
- Title
- how to use pow function in java
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- charat(0).touppercase() java
- Category
- Java
- Title
- islowercase java
- Category
- Java
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- print arraylist java
- Category
- Java
- Title
- java - a program to print open ports
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java jagged array days and months
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- arraylist array
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- how to make a button disapear on click in javafx
- Category
- Java
- Title
- processing play sound
- Category
- Java
- Title
- computeifabsent hashmap java
- Category
- Java
- Title
- how to test how many of one character is in a string java
- Category
- Java
- Title
- sqrt in java
- Category
- Java
- Title
- java program to find prime number between 1 and 100
- Category
- Java
- Title
- try block in java
- Category
- Java
- Title
- how to count number of words in a string
- Category
- Java
- Title
- transformez integer en string
- Category
- Java
- Title
- javafx action event enter key
- Category
- Java
- Title
- how to declare string array in java
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- take string from keyboard java
- Category
- Java
- Title
- How to make a class in Java?
- Category
- Java
- Title
- system.in example in java
- Category
- Java
- Title
- log10 in java
- Category
- Java
- Title
- socket programming in java
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- équivalent setTimeInterval java
- Category
- Java
- Title
- java hashtable
- Category
- Java
- Title
- how to remove all special characters from a string in java
- Category
- Java
- Title
- how to make an object move with arrow keys in java
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- unity how to make a gameobject slowly look at a position
- Category
- Java
- Title
- lastindexof java
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- Unhandled exception: java.lang.InterruptedException
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- How to draw Bezier Curve in Android
- Category
- Java
- Title
- java strin to int
- Category
- Java
- Title
- how to get the max value of an array java
- Category
- Java
- Title
- spring data jpa inheritance repository
- Category
- Java
- Title
- java constructor genertic
- Category
- Java
- Title
- java create txt file
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- how to loop through an array
- Category
- Java
- Title
- android java convert double to 2 decimal places
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- how to set frame colo in java
- Category
- Java
- Title
- java http interceptor
- Category
- Java
- Title
- java dictionary
- Category
- Java
- Title
- system.arraycopy java
- Category
- Java
- Title
- jquery set data attribute value
- Category
- Java
- Title
- Kotlin is like java
- Category
- Java
- Title
- regex get string between quotes java
- Category
- Java
- Title
- java android build secret keys
- Category
- Java
- Title
- how to read a csv file java
- Category
- Java
- Title
- byte java
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- java array to list
- Category
- Java
- Title
- floor in java
- Category
- Java