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...!!! Click To Tweet
Syntax:
1 | Map<String, String> map = new HashMap<String, String>(); |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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:
1 2 3 4 5 6 | 1000 yogi 100 dv 6 2 |
Easiest representation of HashMap in Java...!!! Click To Tweet
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: