Reputation: 45
sorry to say, I'm not very experienced with Java. I'm using eclipse as IDE. Eclispe complains about unchecked cast which I can not understand. Please refer to the code snipet. How to make this right in general ?
public Liberty2Server(ArrayList<Map<String,String>> libertySourceFiles , String basePath) throws FileNotFoundException , IllegalArgumentException {
// check input file path
for( Object map : libertySourceFiles) {
Map<String,String> mp = (Map<String,String>) map ; // <==Eclipse complains here: Unchecked cast from Object to Map<String,String>
Any hint is welcome
Rolf
Upvotes: 0
Views: 191
Reputation: 5686
The problem here is that you upcast the ArrayList<Map<String,String>>
element do Object
and then downcast it to a generic type.
The solution is easy:
public Liberty2Server(ArrayList<Map<String,String>> libertySourceFiles , String basePath) throws FileNotFoundException , IllegalArgumentException {
// check input file path
for( Map<String,String> mp : libertySourceFiles) {
// Map<String,String> mp = (Map<String,String>) map ; Not needed anymore
....
Upvotes: 0
Reputation: 2982
Eclipse complains, because you want to cast an Object
to a Map<String, String>
. Change the type of your running variable map
to Map<String, String>
, and everything will be fine. You won't even need the variable mp
then.
Upvotes: 0
Reputation: 533492
It means you are performing a cast which the compiler cannot check is safe.
It warns you that you have to check it's safe.
A better solution is to remove the need for the cast by using
for(Map<String, String> mp : libertySourceFiles) {
Upvotes: 2
Reputation: 328598
You can change your for loop and use the explicit type in the collection:
for(Map<String,String> map : libertySourceFiles) {
Map<String,String> mp = map; //probably not needed any more
}
Upvotes: 3