"error: cannot find HashMap character"

Trying to create (or rather find out) HashMapbelow:

public class Demo{

     public static void main(String args[]){
        System.out.println("============Starting Hashmap============");


        //hashmap portion
        HashMap<String, Integer> myMap = new HashMap<String, Integer>();

        System.out.println("============Ending Hashmap============");
     }
}

I use an online compiler and searched a lot, I found that my way of declaring is correct, but something else is causing an error.
Below is the error

Demo.java:8: error: cannot find symbol
                HashMap<String, Integer> myMap = new HashMap<String, Integer>();
                ^
   symbol:   class HashMap
   location: class Demo

   Demo.java:8: error: cannot find symbol
                HashMap<String, Integer> myMap = new HashMap<String, Integer>();
                                                     ^
      symbol:   class HashMap
      location: class Demo

2 errors

I need help with: m, just trying to get a basic way to create a hash map and insert some key and value into it, but the above error stopped me in the first step ..... any help in solving this is appreciated! :)

+3
source share
2 answers

You need to import HashMapto class

import java.util.HashMap;

public class Demo{

      public static void main(String args[]){
        System.out.println("============Starting Hashmap============");


        //hashmap portion
        HashMap<String, Integer> myMap = new HashMap<String, Integer>();

        System.out.println("============Ending Hashmap============");
     }
}
+13
source

you need to import a HashMap to avoid a compilation error

import java.util.HashMap;
+2
source

All Articles