HashMap is an object that store item in the form of  “key-value” pair. Here, In this post, we will see “how to create a HashMap object in Java and how to iterates its data?”

Easiest representation of HashMap in Java...!!! Share on X

Syntax:

Map<String, String> map = new HashMap<String, String>();

Example

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HashMapSample {

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();

		map.put("name1", "yogi");
		map.put("id1", "100");
		map.put("age1", "6");
		map.put("name2", "dv");
		map.put("id2", "1000");
		map.put("age2", "2");
		
		Set<String> keys = map.keySet();
		for(String key : keys){
			System.out.println(map.get(key));
		}

	}
}

 

Output:

1000
yogi
100
dv
6
2

 

Easiest representation of HashMap in Java...!!! Share on X

 

If you like this post, please click like button and share it with others on Twitter. Also, check out my other useful blog posts on Java:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts