Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Warning error ..?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all


In this code mapVendorRegions is an hashmap which consists of key as string and value as an arraylist object which holds only string the code is compiled with 2 warnings please let me know to clear warnings
im getting warning as follows:

PreferenceSetupController.java:699: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.Map.Entry<java.lang.String,java.util.ArrayList<java.lang.Str
ing>>
Map.Entry<String
,ArrayList<String>> mapRemainEntry= (Map.Entry<String,ArrayList<String>>)itx.nex
t();
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using the raw type Set in this line:

Do this instead:

Then you also don't need the cast anymore in the line inside the loop:
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir

As you told i had made changes in my code and compiled but still its giving me the same warning
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add the generics to Iterator too.

But better is to not use an Iterator at all and use the Java 5 for-loop syntax:

Try to understand the warning - it says you are casting from Object to Map.Entry<String,ArrayList<String>> and the compiler warns you that it has no way to check if that cast is safe or not.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sir i got with out warning
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not the absence of warnings you should be worrying about. It is the absence of risks of a ClassCastException. If you compile with no warnings and no "suppress warnings" annotations, then you are not risking a ClassCastException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic