Unchecked call hashmap

C

Crouchez

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);
^
 
M

Manish Pandit

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
 
J

Joshua Cranmer

Crouchez said:
Eric Sosman said:
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.
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...
 
E

Eric Sosman

Crouchez wrote On 09/05/07 15:15,:
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.
 
L

Lew

Eric said:
- 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:
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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top