HashMap savedStuff = new HashMap();
savedStuff.put(\"symbol\", this.symbol); //this is a string
savedStuff.put(\"index\", this.index); //this is an int
This is simple code for use of hashmap. There i will use key as integer and value as string type. Map is very useful when our functionality works on key and values pairs. Below is a simple example of use hashmap. I hope it is very useful for all.
public class CreateHashMap {
public static void main(String[] args) {
Map map = new HashMap();
/*
* Associates the specified value with the specified key in
this map (optional operation). If the map previously
contained a mapping for the key, the old value is
replaced by the specified value
*/
map.put(1,"ankush");
map.put(2, "amit");
map.put(3,"shivam");
map.put(4,"ankit");
map.put(5, "yogesh");
//print hashmap
System.out.println("HashMap = "+map);
}
}
Reference : create and use of map