How to get a Map key / value pair using Map.entrySet
.
There are other ways to get Map keys and values: keySet
and values
.
These only get either the key or the value, but you can use entrySet
to get the key / value pair.
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
In the following process, the entrySet method is used to get the key / value pair, the getKey method is used to get the key, and the getValue method is used to get the value.
for (Map.Entry<String, String> animalNameInJapanese : animal.entrySet()) {
System.out.println(animalName.getKey() + "Is in Japanese" + animalName.getValue() + "is.");
}
monkey is a monkey in Japanese.
dog is a dog in Japanese.
cat is a cat in Japanese.
Map Java Platform SE8 #entrySet
Recommended Posts