Java Collection How to - Extract values from HashMap








Question

We would like to know how to extract values from HashMap.

Answer

import java.util.HashMap;
//from ww w  .  ja  v  a 2  s  .  c o  m
public class Main {
  public static void main(String args[]) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

    map.put(1, 2);
    map.put(2, 3);
    map.put(3, 4);

    for (Integer key : map.keySet()) {
      System.out.println(map.get(key));
    }
  }
}

The code above generates the following result.