user3302053
user3302053

Reputation: 81

"TreeMap does not take parameters" Compilation Error

This is my code:

import java.util.*;
public class TreeMap {
    public static void main(String[] args) {
        Map<String,Integer> treemap = new TreeMap<String,Integer>();
        Some code to fill the treemap ie treemap.put("Kevin", 36);
    }
}

And I am getting this compiler error:

TreeMap.java:5: error: type TreeMap does not take parameters
   Map<String,Integer> treemap = new TreeMap<String,Integer>();
                                            ^

Upvotes: 2

Views: 592

Answers (3)

Zhongxia Yan
Zhongxia Yan

Reputation: 106

To clarify Eran's comment, import java.util.* imports everything in java.util, which includes java.util.TreeMap and a bunch of things you're not using. This is usually not good programming practice because it means that there are more names potentially in conflicts with names that you choose to use (like what happened to you above). Instead of importing java.util.* in the future, try importing only the objects that you actually need.

Upvotes: 2

chokdee
chokdee

Reputation: 456

Use full qualified class name for the Java TreeMap class

new java.util.TreeMap<String,Integer>()

Upvotes: 2

Eran
Eran

Reputation: 393801

public class TreeMap

rename your class. It clashed with java.util.TreeMap

The compiler thinks that new TreeMap<String,Integer>() refers to your own class, which doesn't take any parameters.

Upvotes: 9

Related Questions