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

 Live Demo

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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements