java map foreach
Java
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
items.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
if("E".equals(k)){
System.out.println("Hello E");
}
});// 1. for-each with Entry
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());
}
// 2. for-each with key or value each (faster)
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//iterating over keys only
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//iterating over values only
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
// 3. Using Iterator
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// With Generic
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
// No Generic
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
Also in Java:
- Title
- sending a excel in an attachment in email java
- Category
- Java
- Title
- convert int array to integer list java
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java add a list to a list
- Category
- Java
- Title
- java variable declaration
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- java size of stack
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- how to find substring in java
- Category
- Java
- Title
- dice in java
- Category
- Java
- Title
- j
- Category
- Java
- Title
- convert hashset to int array java
- Category
- Java
- Title
- java how to program
- Category
- Java
- Title
- how timer class and display to jtextfield w3schools in java
- Category
- Java
- Title
- java map example
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- expression régulière téléphone java
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- java how to put a string sentence in a map
- Category
- Java
- Title
- java square a number
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- java localdate subtract two dates
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- gson parse json
- Category
- Java
- Title
- java rest client response json
- Category
- Java
- Title
- set color of text for jlabel
- Category
- Java
- Title
- how to add all list elements at once in java
- Category
- Java
- Title
- jbutton remove selection border
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- random boolean java
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- how to get the screen dimensions in java
- Category
- Java
- Title
- decomposer chaine de caractère java
- Category
- Java
- Title
- how to limit double decimal places java
- Category
- Java
- Title
- random java
- Category
- Java
- Title
- java previous permutation
- Category
- Java
- Title
- java queue
- Category
- Java
- Title
- java 8 seconds to days
- Category
- Java
- Title
- java prev_permutation
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- how to get the max value of an array java
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- java android development get element by id
- Category
- Java
- Title
- arraylist java methds
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- convert string to mayus java
- Category
- Java
- Title
- read int from keyboard java
- Category
- Java
- Title
- capacitor-android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- imagesbutton how to set background
- Category
- Java
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- java random integer
- Category
- Java
- Title
- java how to make a string lowercase
- Category
- Java
- Title
- actionListener java
- Category
- Java
- Title
- ndroid.support.v4.content.FileProvider
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- java random number generator in range
- Category
- Java
- Title
- java fileinputstream
- Category
- Java
- Title
- Create ArrayList from array java
- Category
- Java
- Title
- android how to split string
- Category
- Java
- Title
- string isEmpty java
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- java array out of bounds
- Category
- Java
- Title
- Manifest merger failed androidx
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- java prev permutation
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- phoneword converter
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- javafx datepicker
- Category
- Java
- Title
- java how to read a text file
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- import collections in java
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- try block in java
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- reentrantreadwritelock java
- Category
- Java
- Title
- official java website
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- java insert array
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- java swing timer sleep
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- java read file from command line argument
- Category
- Java
- Title
- create notification android
- Category
- Java
- Title
- java hashmap set value
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- Display double in decimal places java
- Category
- Java
- Title
- java observable to observer
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- java Convert a string IPv4 IP address to the equivalent long numeric value.
- Category
- Java