这是如何使用keySet(),get(),values()和entrySet()函数在Map中获取键和值的另一个示例:
Map<Integer, String> testKeyset = new HashMap<Integer, String>();
testKeyset.put(1, "first");
testKeyset.put(2, "second");
testKeyset.put(3, "third");
testKeyset.put(4, "fourth");
// Print a single value relevant to a specified Key. (uses keySet())
for(int mapKey: testKeyset.keySet())
System.out.println(testKeyset.get(mapKey));
// Print all values regardless of the key.
for(String mapVal: testKeyset.values())
System.out.println(mapVal.trim());
// Displays the Map in Key-Value pairs (e.g: [1=first, 2=second, 3=third, 4=fourth])
System.out.println(testKeyset.entrySet());