Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java program runs fine but doesn't compile

I have a java program which runs properly.

But when I try to clean and build it in Netbeans it is choking on this line:

 protected HashMap<String, ArrayList<HashMap<String,String>>> config1

 config1 = new <String,ArrayList<HashMap<String,String>>> HashMap(); // build breaks here.

the error is:

  cannot find symbol  
  symbol  : constructor     
  <java.lang.String,java.util.ArrayList<java.util.HashMap<java.lang.String,java.lang.String>>
  >HashMap()
like image 590
user492883 Avatar asked Dec 15 '22 17:12

user492883


1 Answers

You are placing your type parameters in wrong place. It comes in between HashMap and the (): -

config1 = new HashMap<String,ArrayList<HashMap<String,String>>>();

Also, its a good idea to have more generalized types rather than specific types in the declaration, and even in generic type parameters. So you should use Map instead of HashMap in declaration, and List instead of ArrayList in your type parameter: -

And actually, you don't need to break your declaration and initialization in two lines. Just have them in one single line. It looks more cleaner. So, you can change your two lines to: -

protected Map<String, List<Map<String,String>>> config1 = 
                               new HashMap<String, List<Map<String,String>>>();
like image 166
Rohit Jain Avatar answered Dec 27 '22 05:12

Rohit Jain