Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"error: cannot find symbol HashMap" [duplicate]

Trying to create (or rather learn) a HashMap in below fashion :

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 am using an online complier and have searched a lot, i found that my way of declaration is correct but something else is popping up the 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

What i need help in : m just trying to get the basic of creating a hashmap and inserting some key and value in it, but above error stopped me in very first step.....any help in solving this is appreciated!! :)

like image 747
NoobEditor Avatar asked Feb 12 '14 07:02

NoobEditor


3 Answers

You need to import the HashMap into the 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============");
     }
}
like image 144
Abimaran Kugathasan Avatar answered Oct 20 '22 08:10

Abimaran Kugathasan


you need to import the HashMap to avoid the compile error

import java.util.HashMap;
like image 30
Nambi Avatar answered Oct 20 '22 09:10

Nambi


java.util.HashMap<Character, Integer> map = new java.util.HashMap<>();

Use this if you can't import java.util.HashMap;

like image 1
Jayasurya Avatar answered Oct 20 '22 10:10

Jayasurya