How to Iterate Through Map and List in Java? Example attached (Total 5
How to Iterate Through Map and List in Java? Example attached (Total 5 from crunchify.com

Introduction

If you are a Java developer, then you must be familiar with the Map data structure. It is one of the most commonly used data structures in Java, and it is used to store key-value pairs. In this article, we will discuss how to iterate over a Map in Java.

What is Map?

Map is an interface in Java that represents a mapping between a key and a value. It allows you to store key-value pairs, where each key is unique, and the value can be duplicated. There are several implementations of the Map interface in Java, such as HashMap, TreeMap, LinkedHashMap, and others.

Why Iterate over a Map?

Iterating over a Map is important because it allows you to access all the keys and values stored in the Map. This can be useful in various scenarios, such as printing all the key-value pairs, filtering the Map based on certain conditions, or performing some operations on the values.

How to Iterate over a Map?

There are several ways to iterate over a Map in Java, such as using the keySet(), entrySet(), or values() methods. Let’s discuss each of them in detail.

Using keySet() Method

The keySet() method returns a Set view of the keys stored in the Map. You can use this Set to iterate over all the keys and access their corresponding values using the get() method. Here’s an example: “` Map map = new HashMap<>(); map.put(“apple”, 1); map.put(“banana”, 2); map.put(“orange”, 3); for (String key : map.keySet()) { Integer value = map.get(key); System.out.println(key + ” =” + value); } “` This will print the following output: “` apple = 1 banana = 2 orange = 3 “`

Using entrySet() Method

The entrySet() method returns a Set view of the key-value pairs stored in the Map as Map.Entry objects. You can use this Set to iterate over all the entries and access their keys and values using the getKey() and getValue() methods. Here’s an example: “` Map map = new HashMap<>(); map.put(“apple”, 1); map.put(“banana”, 2); map.put(“orange”, 3); for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key + ” =” + value); } “` This will print the same output as the previous example: “` apple = 1 banana = 2 orange = 3 “`

Using values() Method

The values() method returns a Collection view of the values stored in the Map. You can use this Collection to iterate over all the values and access their corresponding keys using the keySet() method. Here’s an example: “` Map map = new HashMap<>(); map.put(“apple”, 1); map.put(“banana”, 2); map.put(“orange”, 3); for (Integer value : map.values()) { for (String key : map.keySet()) { if (map.get(key).equals(value)) { System.out.println(key + ” =” + value); } } } “` This will also print the same output: “` apple = 1 banana = 2 orange = 3 “`

Conclusion

In this article, we have discussed how to iterate over a Map in Java using the keySet(), entrySet(), and values() methods. Depending on your requirements, you can choose the appropriate method to access the keys and values stored in the Map. Iterating over a Map is important for various scenarios, such as printing all the key-value pairs, filtering the Map based on certain conditions, or performing some operations on the values.