Java Map iterate via key-value entry set

Description

Java Map iterate via key-value entry set


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Main {

  public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("3", "Java");
    map.put("1", "CSS");
    map.put("4", "Javascript");
    map.put("2", "HTML");

    Set<Map.Entry<String, String>> s = map.entrySet();
    Iterator<Map.Entry<String, String>> it = s.iterator();
    //from   w  ww  .  ja v a 2s.  co  m
    while (it.hasNext()) {
      Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
      String key = (String) entry.getKey();
      String value = (String) entry.getValue();
      System.out.println(key + " => " + value);
    }
  }
}



PreviousNext

Related