Java program works fine but does not compile

I have a java program that works correctly.

But when I try to clean and build it in Netbeans, it suffocates on this line:

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

error:

  cannot find symbol symbol : constructor <java.lang.String,java.util.ArrayList<java.util.HashMap<java.lang.String,java.lang.String>> >HashMap() 
+4
source share
4 answers

You put your type parameters in the wrong place. It is between the HashMap and () : -

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

It is also a good idea to have more general types, rather than specific types in the declaration, and even in generic type parameters . Thus, you should use Map instead of HashMap in the declaration, and List instead of ArrayList in type parameter : -

And in fact, you do not need to break the declaration and initialization in two lines. Just have them on one line. It looks cleaner. So you can change two lines: -

 protected Map<String, List<Map<String,String>>> config1 = new HashMap<String, List<Map<String,String>>>(); 
+5
source

You must put the class name in front of the generics.

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

Generics must follow the class name. It cannot be used before the class name. Correct the second line as shown below:

  protected HashMap<String, ArrayList<HashMap<String,String>>> config1; config1 = new HashMap <String,ArrayList<HashMap<String,String>>>(); 
+2
source

You can try the following:

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

Source: https://habr.com/ru/post/1446871/


All Articles