Home >Java >javaTutorial >Java Iterate Map
Iterate Map is extracting a bunch of data from the Map collection. In most real-time cases, we have much data stored on a map. The developer wants to fetch this data; he has to iterate the entire map. A map is stored in util.Map package. In this topic, we are going to learn about Java Iterate Map.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
You can iterate over a map in 3 ways.
1. Map is a key and value pair structured collection. The map is an interface. So, Map implemented into
2. Entry is the sub interface of Map. Therefore, it will be accessible by the Entry name. It returned the collection view of the map. Map. The entry provides methods to fetch key and value pair.
As discussed above, Map can be iterated with forEach, normal for, and while loop. We will understand it by Syntax.
Syntax:
Map<DataType, DataType>map = new HashMap<DataType, DataType>(); map.forEach((key, value) -> //logic
Syntax:
Map<DataType,DataType> map= new HashMap<DataType,DataType>(); for (Map.Entry<DataType,DataType> set: map.entrySet()) //logic }
Syntax:
Map<String, String> mapObj = new HashMap<>(); Iterator iterator= mapObj .entrySet().iterator(); while (iterator.hasNext()) { //logic }
Based on the requirement, we can iterate the map in any of the above ways.
Given below are the examples:
Code:
import java.util.HashMap; import java.util.Map; public class ForEachMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); // iterating key and value with forEach loop names.forEach((key, value) -> { System.out.println("ID =>" + key + " Name => " + value); }); } }
Output:
Explanation:
As you can see in the above code, we have iterated the map with forEach
Code:
package com.map; import java.util.HashMap; import java.util.Map; public class ForEachKeyValueMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); // iterating ids with forEach loop names.forEach((key, value) -> { System.out.println("ID =>" + key); }); // iterating names with forEach loop names.forEach((key, value) -> { System.out.println("Name => " + value); }); } }
Output:
Explanation:
As you can see in the above output, we can also fetch the key and values separately with the forEach loop.
Code:
<strong> </strong>import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class ForMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); // iterating key and value with for loop for (Entry<Integer, String> entry : names.entrySet()) System.out.println("ID => " + entry.getKey() + " Name => " + entry.getValue()); } }
Output:
Explanation:
As you can see in the above code, we have iterated the map with for loop.
Code:
<strong> </strong>import java.util.HashMap; import java.util.Map; public class ForKeyAndValueMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); //fetching ids for (Integer id : names.keySet()) System.out.println("ID => " + id); // fetching names for (String name : names.values()) System.out.println("Name => " + name); } }
Output:
Explanation:
As you can see in the above output, we can also fetch the key and values separately with for loop.
Code:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class WhileIteratorLoopMap { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); //get entry set from map Set set = names.entrySet(); //get iterator from set Iterator iterator = set.iterator(); //fetching id and names with while loop while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); System.out.println("ID =>"+entry.getKey() + " Name => " + entry.getValue()); } } }
Output:
Explanation:
As you can see in the above code, we have iterated the map with an iterator and while loop.
Note: All the cases produce the same output, but the forEach loop is recommended for development because it has less run time.You can iterate over the map using the for loop, forEach loop, and while loop from the Entry interface. We can also iterate keys and values separately without any errors.
The above is the detailed content of Java Iterate Map. For more information, please follow other related articles on the PHP Chinese website!