Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Unchecked call hashmap

Thread view: 
Crouchez - 05 Sep 2007 18:21 GMT
What is this with javac -Xlint??

warning: [unchecked] unchecked call to put(K,V) as a memb
er of the raw type java.util.HashMap
               headers.put(key,val);
                          ^
Manish Pandit - 05 Sep 2007 18:45 GMT
On Sep 5, 10:21 am, "Crouchez" <b...@bllllllahblllbllahblahblahhh.com>
wrote:
> What is this with javac -Xlint??
>
> warning: [unchecked] unchecked call to put(K,V) as a memb
> er of the raw type java.util.HashMap
>                 headers.put(key,val);
>                            ^

How is headers declared ? You should "type" the key and value in the
map like:

HashMap<Sometype,SomeOtherType> headers = new
HashMap<SomeType,SomeOtherType>();

headers.put(key, val); //where key instanceof SomeType == true and
value instanceof SomeOtherType == true

-cheers,
Manish
Eric Sosman - 05 Sep 2007 19:08 GMT
Crouchez wrote On 09/05/07 13:21,:
> What is this with javac -Xlint??
>
> warning: [unchecked] unchecked call to put(K,V) as a memb
> er of the raw type java.util.HashMap
>                 headers.put(key,val);
>                            ^

http://java.sun.com/docs/books/tutorial/java/generics/index.html

Signature

Eric.Sosman@sun.com

Crouchez - 05 Sep 2007 20:15 GMT
> Crouchez wrote On 09/05/07 13:21,:
>> What is this with javac -Xlint??
[quoted text clipped - 5 lines]
>
> http://java.sun.com/docs/books/tutorial/java/generics/index.html

I'm trying to stay away from Generics. Is it really worth that extra coding
effort? It makes you're code look ugly as well.
Joshua Cranmer - 05 Sep 2007 21:50 GMT
>> Crouchez wrote On 09/05/07 13:21,:
>>> What is this with javac -Xlint??
[quoted text clipped - 7 lines]
> I'm trying to stay away from Generics. Is it really worth that extra coding
> effort? It makes you're code look ugly as well.
For the cost of "ugly" code (C++ templates can go much uglier), you get
compile-time errors instead of runtime errors for some parts of your
code, most notably in the Collections interface. Unless you are doing
awkward tricks or pushing the edges of generics, generics might save you
 some significant debugging time...

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Eric Sosman - 05 Sep 2007 22:55 GMT
Crouchez wrote On 09/05/07 15:15,:

>>Crouchez wrote On 09/05/07 13:21,:
>>
[quoted text clipped - 9 lines]
> I'm trying to stay away from Generics. Is it really worth that extra coding
> effort? It makes you're code look ugly as well.

   Aesthetics aside, your attempt to stay away from
generics has already failed: The Collection framework
has been generified, and the warning message you've
received means your code hasn't kept pace.  As I see
it, you have various possible recourses:

   - Revert to a pre-1.5 Java environment.  This will
     become untenable when 1.4 reaches end-of-life,
     which (IIRC) will be before the USA elects its
     next President.

   - Add annotations (another 1.5 feature; see the
     Tutorial) to suppress the warnings.  This will
     require extra caution on your part, because a
     ClassCastException will make you look reckless
     instead of just unfortunate.

   - Get rid of -Xlint and ignore the warnings that
     still remain.  See above.

   - Adopt generics in your own code, at least to
     the extent of catering to them in other classes
     that are already generified.  Who knows?  You
     might grow accustomed to them, even if they
     never arouse your adoration.

  The choice isn't mine to make.

Signature

Eric.Sosman@sun.com

Lew - 05 Sep 2007 23:18 GMT
>     - Revert to a pre-1.5 Java environment.  This will
>       become untenable when 1.4 reaches end-of-life,
>       which (IIRC) will be before the USA elects its
>       next President.

It's already moribund:
<http://java.sun.com/j2se/1.4.2/>:
> J2SE 1.4.2 has begun the Sun End of Life (EOL) process. The EOL transition period is from Dec, 11 2006, until the General Availability (GA) of the next Java version, Java SE 7, currently planned for the summer of 2008. With this notice, customers are strongly encouraged to migrate to the current release, Java SE 6.

The USA has undoubtedly "elected" its next President, only the people there
haven't been told who they voted for yet.  They should really let the
Electoral College do its representative thing.

Signature

Lew

Roedy Green - 06 Sep 2007 05:29 GMT
On Wed, 05 Sep 2007 19:15:34 GMT, "Crouchez"
<blah@bllllllahblllbllahblahblahhh.com> wrote, quoted or indirectly
quoted someone who said :

>I'm trying to stay away from Generics. Is it really worth that extra coding
>effort? It makes you're code look ugly as well.

Agreed, the generics syntax is barf-inducing, particularly when you
get into < > nests, however, for a simple HashMap<String,Integer> it
is invaluable documentation and will flush out all kinds of bugs. It
also gets rid of explicit casts.  This is a good thing. You should
only specify the type in one place. Java generics have it down to two.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 06 Sep 2007 04:41 GMT
eOn Wed, 05 Sep 2007 17:21:59 GMT, "Crouchez"
<blah@bllllllahblllbllahblahblahhh.com> wrote, quoted or indirectly
quoted someone who said :

>What is this with javac -Xlint??
>
>warning: [unchecked] unchecked call to put(K,V) as a memb
>er of the raw type java.util.HashMap
>                headers.put(key,val);
>                           ^

redo the compile with
javac -Xlint  *.java
and you will get a better error message.

See http://mindprod.com/jgloss/generics.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.