Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type HashMap does not take parameters

Tags:

java

shadowing

I was following this video about HashMap in java. it has below code.

// Create the HashMap
HashMap<String,String> hm = new HashMap<String, String>();

// Put data
hm.put("Katie", "Android, WordPress");
hm.put("Magda", "Facebook");
hm.put("Vanessa", "Tools");
hm.put("Ania", "Java");
hm.put("Ania", "JEE");    // !! Put another data under the same key, old value is overridden

// HashMap iteration
for (String key: hm.keySet())
    System.out.println(key+":"+hm.get(key));

so I wrote my below code, using it to practice HashMap (almost same code)

package hashmap;
import java.util.*;

public class HashMap {

    public static void main(String[] args) {

        HashMap<String,String> hm = new HashMap<String, String>();

        hm.put("Katie", "Android, WordPress");
        hm.put("Magda", "Facebook");
        hm.put("Vanessa", "Tools");
        hm.put("Ania", "Java");
        hm.put("Ania", "JEE");   

    }
}

But the class didn't compile give error "Type HashMap does not take parameters" So I searched for answers where I got this

one of answers says

Two possible mistakes:

You are using JDK 1.4

You imported something else than java.util.Map

So I imported java.util.Map but netbeans gives that error and say the import has not used. Then I java.util.*; but result was same. I don't know if this is novice mistake of my IDE fault.

My jdk 1.8 and Netbeans 8.0.2 in windows 8.1

like image 263
Menuka Ishan Avatar asked Dec 14 '22 10:12

Menuka Ishan


1 Answers

You're naming your class HashMap which is shadowing the java.util.HashMap. Just rename it to something else.

like image 65
M A Avatar answered Dec 16 '22 22:12

M A