Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Iterate over a HashMap - Testingpool

Iterate over a HashMap

In this post, we have seen how to add the element to HashMap. In this post, we will see how to iterate over a HashMap.

We can iterate over a HashMap by using two ways.

  • For loop
  • Iterator

It implements an interface Map.Entry to get the collection of key and value pair.

Syntax:

public static interface Map.Entry<K,V>

A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view.

It has following methods to read the key and value.

getKey() : Returns the key corresponding to this entry.

getValue(): Returns the value corresponding to this key entry.

Example with for loop:

import java.util.HashMap;
import java.util.Map;

public class HashMapEx {

	public static void  main(String[] args){
		
		HashMap<Integer,String> hm = new HashMap<Integer,String>();
		hm.put(1, "Mobile");
		hm.put(6, "TV");
		hm.put(10, "Laptop");
		hm.put(2, "Desktop");
		hm.put(15, "Tablet");
		
		for(Map.Entry m : hm.entrySet()){
			System.out.println(m.getKey()+"----"+m.getValue());
		}
	}
}

Output:

1—-Mobile
2—-Desktop
6—-TV
10—-Laptop
15—-Tablet

Example with Iterator:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapEx {

	public static void  main(String[] args){
		
		HashMap<Integer,String> hm = new HashMap<Integer,String>();
		hm.put(1, "Mobile");
		hm.put(6, "TV");
		hm.put(10, "Laptop");
		hm.put(2, "Desktop");
		hm.put(15, "Tablet");
		
		Iterator iter = hm.entrySet().iterator();
		while(iter.hasNext()){
			Map.Entry m = (Map.Entry)iter.next();
			System.out.println(m.getKey()+"----"+m.getValue());
		}
	}
}

Output:

1—-Mobile
2—-Desktop
6—-TV
10—-Laptop
15—-Tablet


Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...