Extract values from HashMap in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 25-Apr-2019 09:03:35

Advertisements