Enhanced for loop in java Map: How to Get keys and values of the map
Description:
Enhanced for loop in java Map- Have you ever wanted to get the key and value from Map in Java? To get data from Map, it is convenient to use loop processing such as for statements and the entrySet method! This time, besides the for statement, I will explain how to get data from Map with enhanced for loop and Iterator loop processing.
In this article, we will learn
- What is for statement and for each statement (enhanced for loop statement)?
- How to get the Map key (key) with the keySet method
- How to get the value of Map with the values ​​method
From the basic content, I will also explain applied usage such as “What is Iterator?” This time, to remember these methods, I will explain in an easy-to-understand manner how to get the key and value using the for statement and foreach loop in Map!
How to use for Loop and enhanced for loop in java:
Java doesn’t have the foreach statement that other languages ​​use!
However, there is an enhanced statement that does something similar to the foreach statement! The enhanced for loop statement is used as follows.
Basic Syntax of Enhanced For Loop in java map:
for (type variable name : List name) {Â Â
System .out .println (variable name);
}
comparison between For loop and enhanced for loop
To learn how to use the enhanced for loop, compare it with the for statement in the following program and check how it is used.
package com.mycompany.javabasics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Fawadkhan
 */
public class MapExamples {
public static void main(String[] args) {
       List<String> list = new ArrayList<>();
list.add(“Programming”);
list.add(“Digest”);
       // How to use enhanced for loop statement (for-each statement)
System.out.println(“Using Enhanced for loop statement”);
for (String s : list) {
System.out.println(s);
       }
       // how to use simple for loop statement
System.out.println(“Using Simple for loop statement!”);
for (inti = 0; i<list.size(); i++) {
System.out.println(list.get(i));
       }
   }
}
Output:
Here i use the enhanced for loop and the for statement to display all the elements of the List in order. By using the enhanced for loop statement like this, you can write clean code like the foreach statement in other languages!
Get the key and value of Map with enhanced for loop
How to get Map key (key) with keySet method
Here, I will explain how to get the keys of the Map in order using the enhanced for loop. In the following program, the keys of the Map are obtained by the keySet method and displayed in order by the enhanced for loop statement.
package com.mycompany.javabasics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Fawadkhan
 */
public class MapExamples {
public static void main(String[] args) {
       Map<String, String> map = new HashMap<>();
map.put(“Name”, “Fawad khan”);
map.put(“Website”, “Programmingdigest”);
for (String key : map.keySet()) {
System.out.println(key);
       }
   }
}
Output:
By using the keySet method and the enhanced for loop like this, we were able to get the Map key!
How to get the value of Map with the values ​​method
Next, I will explain how to get the values ​​of the Map in order using the enhanced for loop in java. In the following program, the values ​​of Map are obtained by the values ​​method and displayed in order by the enhanced for loop statement.
package com.mycompany.javabasics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Fawadkhan
 */
public class MapExamples {
public static void main(String[] args) {
       Map<String, String> map = new HashMap<>();
map.put(“Name”, “Fawad khan”);
map.put(“Website”, “Programmingdigest”);
for (String key : map.values()) {
System.out.println(key);
       }
   }
}
Output:
By using the values ​​method and the enhanced for loop statement like this, we were able to get the values ​​of the Map!
How to get both key and value of Map with entrySet method
Finally, I’ll show you how to get both the keys and values ​​of a Map in order using an enhanced for loop in java. The following program gets the key-value pairs (entries) of the Map with the entrySet method.
After that, using the getKey method and getValue method in the enhanced for loop statement, the key and value are obtained and displayed in order.
package com.mycompany.javabasics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Fawadkhan
 */
public class MapExamples {
public static void main(String[] args) {
       Map<String, String> map = new HashMap<>();
map.put(“Name”, “Fawad khan”);
map.put(“Website”, “Programmingdigest”);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ” : ” + entry.getValue());
       }
   }
}
Output:
By using the entrySet method and the enhanced for loop statement like this, we were able to get the keys and values ​​of the Map!
Get Map keys and values ​​with Iterator loop
What is an Iterator
An iterator is a function that processes all elements in order, such as a List and Map. A Map doesn’t have an index like a List, but you can use an Iterator to go through all the elements!
An Iterator keeps processing until there are no more elements, so you can use it without worrying about how many elements there are!
How to get Map key and value in Iterator loop
Finally, I will explain how to get the keys and values ​​of the Map using Iterator. Let’s look at the following program to learn how to use Iterator in java.
package com.mycompany.javabasics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Fawadkhan
 */
public class MapExamples {
public static void main(String[] args) {
       Map<String, String> map = new HashMap<>();
map.put(“Name”, “Fawad khan”);
map.put(“Website”, “Programmingdigest”);
for (Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + ” : ” + entry.getValue());
       }
   }
}
 Output:
Here, all key-value pairs (entries) of Map are set in Iterator by map.entrySet().iterator(), and iterator.hasNext() loops until there is no next element. . Then iterator.next() gets a set of key-value pairs.
Finally, the key and value are obtained and displayed by the getKey method and getValue method from the key-value pair.
Summary
How was it? This time, I explained how to get the key and value of Map by loop processing. Please remember the enhanced for loop in java, because it allows you to write code concisely. Check out this article if you forgot to loop over the Map!