Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Extract values from HashMap in Java
To extract values from HashMap, let us first create a HashMap with keys and values −
HashMap<Integer, Integer>m = new HashMap<Integer, Integer>();
Now, add some elements to the HashMap −
m.put(10, 20); m.put(30, 40); m.put(50, 60); m.put(70, 80); m.put(90, 100); m.put(110, 120); m.put(130, 140); m.put(150, 160);
Now, extract the values from the HashMap −
for (Integer i: m.keySet()) {
System.out.println(m.get(i));
}
Example
import java.util.HashMap;
public class Demo {
public static void main(String args[]) {
HashMap<Integer, Integer>m = new HashMap<Integer, Integer>();
m.put(10, 20);
m.put(30, 40);
m.put(50, 60);
m.put(70, 80);
m.put(90, 100);
m.put(110, 120);
m.put(130, 140);
m.put(150, 160);
System.out.println("Displaying values from HashMap...");
for (Integer i: m.keySet()) {
System.out.println(m.get(i));
}
}
}
Output
Displaying values from HashMap... 60 140 80 160 20 100 40 120
Advertisements
