Unchecked call hashmap  
Author Message
Crouchez





PostPosted: 2007-9-6 1:22:00 Top

java-programmer, Unchecked call hashmap 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





PostPosted: 2007-9-6 1:46:00 Top

java-programmer >> Unchecked call hashmap On Sep 5, 10:21 am, "Crouchez" <email***@***.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





PostPosted: 2007-9-6 2:08:00 Top

java-programmer >> Unchecked call hashmap 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

--
email***@***.com
 
 
Crouchez





PostPosted: 2007-9-6 3:16:00 Top

java-programmer >> Unchecked call hashmap
"Eric Sosman" <email***@***.com> wrote in message
news:1189015687.400080@news1nwk...
> 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
>
> --
> email***@***.com

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





PostPosted: 2007-9-6 4:50:00 Top

java-programmer >> Unchecked call hashmap Crouchez wrote:
> "Eric Sosman" <email***@***.com> wrote in message
> news:1189015687.400080@news1nwk...
>> 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
>>
>> --
>> email***@***.com
>
> 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...


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





PostPosted: 2007-9-6 5:56:00 Top

java-programmer >> Unchecked call hashmap Crouchez wrote On 09/05/07 15:15,:
> "Eric Sosman" <email***@***.com> wrote in message
> news:1189015687.400080@news1nwk...
>
>>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
>
>
> 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.

--
email***@***.com
 
 
Lew





PostPosted: 2007-9-6 6:18:00 Top

java-programmer >> Unchecked call hashmap Eric Sosman wrote:
> - 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.

--
Lew
 
 
Roedy Green





PostPosted: 2007-9-6 11:41:00 Top

java-programmer >> Unchecked call hashmap eOn Wed, 05 Sep 2007 17:21:59 GMT, "Crouchez"
<email***@***.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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Roedy Green





PostPosted: 2007-9-6 12:30:00 Top

java-programmer >> Unchecked call hashmap On Wed, 05 Sep 2007 19:15:34 GMT, "Crouchez"
<email***@***.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.

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