java map iteration
Java
// 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
- mkyong restful web services spring boot examples post
- Category
- Java
- Title
- java queue
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- how to show the hex detail of a file in java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- java how to iterate through a arraylist
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- sort a map based on keys and values using java 8
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- find the day of the week Calendar class in java
- Category
- Java
- Title
- left shift in java
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- entre clavier java
- Category
- Java
- Title
- how to loop through an array
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- shortcut to find a class in java project eclipse
- Category
- Java
- Title
- how to change all the text in a textArea java at once
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- elasticsearch api code call using highlevelrestclient
- Category
- Java
- Title
- nextchar in java
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- how to split a string in java
- Category
- Java
- Title
- spigot run task later
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- java long to int
- Category
- Java
- Title
- how to multiply a number by itself using for loop in java
- Category
- Java
- Title
- converting char array to string
- Category
- Java
- Title
- java how to program
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- to char array java
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- java thread
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- how to get orientation lock to portrait android stackoverflow
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- ecommerce app github android
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- javafx textarea size
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- java resource file
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- java loop through map
- Category
- Java
- Title
- how to read file from assets folder in android
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- java variable string
- Category
- Java
- Title
- java 8 collect multiple lists into single list
- Category
- Java
- Title
- demo java file
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- how to make a button disapear on click in javafx
- Category
- Java
- Title
- how to add integers in java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- java mockito any string import
- Category
- Java
- Title
- priorityqueue poll
- Category
- Java
- Title
- java save string in text file
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- java script zip function
- Category
- Java
- Title
- java == vs equals
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- set password visible in android
- Category
- Java
- Title
- connect 2 package in android
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- fibonacci sequence java using recursion
- Category
- Java
- Title
- java random between two strings
- Category
- Java
- Title
- rewrite file java
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- random number in range java
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- array of objects in java
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- pattern.compile java
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- value receive null with post method in the java spring controller
- Category
- Java
- Title
- how to separate no and text in java
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- writing to a text file java
- Category
- Java
- Title
- how to ask user for his location in android
- Category
- Java
- Title
- j
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- 3-way radix quicksort java
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- how to do substring java
- Category
- Java