Map key

In this chapter you will learn:

  1. How to get all key values from a Map

Map key set

Other than getting key value pair from Map.Entry, we can also get the all key values as a set.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
//  j  ava 2s .  co m
public class MainClass {

  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:

Next chapter...

What you will learn in the next chapter:

  1. How to get value set from a Map
  2. How to get a value from Map
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap