/** * @author lautturi.com * Java example: how to print a map in java */ import java.util.*; public class Lautturi { public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "Java"); map.put(2, "Python"); map.put(3, "C++"); map.put(4, "1autturi.com"); map.put(5, "JavaScript"); // java only print the keys in the map for (Integer key : map.keySet()) { System.out.println("key:" + key); } // method 1 System.out.println("---- java print map method 1 ---"); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } // method 2 System.out.println("---- java print map method 2 ---"); Set<Integer> keySet = map.keySet(); Iterator<Integer> iter = keySet.iterator(); while (iter.hasNext()) { Integer key = iter.next(); System.out.println(key + ":" + map.get(key)); } } }So.www:ecrulautturi.com
output:
key:1 key:2 key:3 key:4 key:5 ---- java print map method 1 --- 1 Java 2 Python 3 C++ 4 1autturi.com 5 JavaScript ---- java print map method 2 --- 1:Java 2:Python 3:C++ 4:1autturi.com 5:JavaScript