• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Error "incompatible types" when creating a Map  RSS feed

 
Greenhorn
Posts: 3
Linux Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a variable



which I initialize like this:



and, using javac 1.7.0_79, get the error message incompatible types. Why? After all, a HashMap is a special kind of a map, and an ArrayList is a special kind of a List.
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, but when initializing the map, the formal types of the arguments must match -- you can't use subtypes. The class itself can be a subtype, though.

Translation: You are correct to use HashMap, but it should be List<String>, not ArrayList<String>. That's because the declaration allows for all types of Lists, and you can't alter the declaration.
 
Bartender
Posts: 1461
31
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ronald can finesse the issue entirely by using this to create his map instance:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True; I forget about that, because I am stuck in 1.6 land. (And still need to support 1.5).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!