GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — How to Iterate Any Map in Java? - GeeksforGeeks How to Iterate Any Map in Java?Last Updated : 1 Sep, 2026 A Map in Java stores data in the form of key-value pairs. Java provides several ways to iterate over a map, depending on whether we need both keys and values or only one of them.
Ways to Iterate Over a MapThe following are the common ways to iterate over a Map in Java. 1. Using entrySet() with For-Each LoopThe entrySet() method returns a set containing all key-value pairs of the map. Each pair is represented by Map.Entry<K, V>.This approach is preferred when we need both the key and value.
Output Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88 Explanation: entrySet() provides each key-value pair as a Map.Entry object. The getKey() method returns the key, while getValue() returns its corresponding value. 2. Using keySet() and values()The keySet() method returns all keys of the map, while the values() method returns all values. This approach is useful when we need only keys or only values.
Output Keys: Akshat Rahul Priya Aman Values: 85 78 92 88 Explanation: keySet() is used to iterate through the keys, whereas values() is used to iterate through the values. If both key and value are required together, entrySet() is generally more suitable. 3. Using IteratorWe can use an Iterator with the entrySet() view of a map. This approach is useful when we need more control over the iteration, such as removing an entry safely during iteration.
Output Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88 Explanation: The Iterator traverses the entries returned by entrySet(). The hasNext() method checks whether another entry is available, and next() returns the next entry. 4. Using forEach()Java 8 introduced the forEach() method, which allows us to iterate over a map using a lambda expression. It provides a short and readable way to process key-value pairs.
Output Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88 Explanation: The forEach() method accepts an action that is performed for every key-value pair. Here, name represents the key and mark represents the value. 5. Using keySet() with get()Another way is to iterate through the keys using keySet() and retrieve the corresponding value using get().
Output Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88 Explanation: In this approach, keySet() provides each key and get(key) is used to obtain its corresponding value. When both key and value are required, entrySet() is generally a simpler choice. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |