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 XSyntax:
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:
Thanks for saving me time. I was looking for such a light example.
thank you Esther.