Java Collection Tutorial - Java Map.keySet()








Syntax

Map.keySet() has the following syntax.

Set < K > keySet()

Example

In the following code shows how to use Map.keySet() method.

//from w w w . j a  v a2  s . co m
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

  public static void main(String[] argv) {
    Map map = new HashMap();

    map.put("Adobe", "Mountain View, CA");
    map.put("IBM", "White Plains, NY");
    map.put("Learning Tree", "Los Angeles, CA");

    Iterator k = map.keySet().iterator();
    while (k.hasNext()) {
      String key = (String) k.next();
      System.out.println("Key " + key + "; Value " + (String) map.get(key));
    }
  }
}

The output: