1
2//Import Hashmap
3import java.util.HashMap;
4
5 HashMap<String, String> dir = new HashMap<String, String>();
6//Add key, values
7 dir.put("hi", "hello");
8 dir.put("wow", "amazing");
9//print value for hi.
10 System.out.println(dir.get("hi");
1import java.util.HashMap;
2//Within a class
3//You can do new HashMap<Key Type, Value Type>();, but you don't need to
4HashMap<Int, String> examplehashmap=new HashMap<>();
5{
6//put in values
7 examplehashmap.put(5, "example");
8};
9//get value
10examplehashmap.get(5);
11//returns "example"
1package com.tutorialspoint;
2
3import java.util.*;
4
5public class HashMapDemo {
6 public static void main(String args[]) {
7
8 // create hash map
9 HashMap newmap = new HashMap();
10
11 // populate hash map
12 newmap.put(1, "tutorials");
13 newmap.put(2, "point");
14 newmap.put(3, "is best");
15
16 // get value of key 3
17 String val = (String)newmap.get(3);
18
19 // check the value
20 System.out.println("Value for key 3 is: " + val);
21 }
22}