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 with the help of ArrayList?”

Easiest representation of HashMap example in Java using ArrayList...!!! 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"); //name1 - key & yogi - value
		map.put("id1", "100");
		map.put("age1", "6");
		map.put("name2", "dv");
		map.put("id2", "1000");
		map.put("age2", "2");

		List<Map<String, String>> keysString = Arrays.asList(map);
		
        for(Map<String, String> key : keysString) 
        {
		       System.out.println(key);
	    }
	}
}

 

Output:

{id2=1000, id1=100, name2=dv, name1=yogi, age2=2, age1=6}

 

Easiest representation of HashMap example in Java using ArrayList...!!! 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