Some help with unchecked types

L

Le Tubs

Hi

I'm relatively new to Java (from a C/C++ background, so please don't
flame me). I having trouble with some warnings like (the code compiles
_but_ I treat warnings as errors).

[javac] Compiling 10 source files to D:\class
[javac] xxxx\Oracle.java:294: warning: [unchecked] unchecked
[javac] found : java.util.Iterator
[javac] required: java.util.Iterator<java.lang.String>
[javac] Iterator<String> it =
this.hashmap.keySet().iterator();

The Code that generating it is below

Now I've gone out onto the www and a quite extensive search and sort of
getting quite confused about the whole issue. Can somebody shed some
light on what I am missing.
Thanking you in advance for your time and consideration.

Thanks
David

TreeMap hashmap;
public ArrayList getSortedKeys()
{
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> it = this.hashmap.keySet().iterator();
while (it.hasNext())
{
keys.add( (String)it.next() );
}
return keys;
}
 
J

Joe Attardi

[javac] found : java.util.Iterator
[javac] required: java.util.Iterator<java.lang.String>
[javac] Iterator<String> it = this.hashmap.keySet().iterator();

Now I've gone out onto the www and a quite extensive search and sort of
getting quite confused about the whole issue. Can somebody shed some
light on what I am missing.
Thanking you in advance for your time and consideration.

Your problem is here:
TreeMap hashmap;

If you want an Iterator<String> with no warnings, you'll need to do
this:
TreeMap<String, Object>

where the second parameter, Object, is whatever type the String keys in
the TreeMap will map to.
 
T

Timbo

Le said:
Hi

I'm relatively new to Java (from a C/C++ background, so please don't
flame me). I having trouble with some warnings like (the code compiles
_but_ I treat warnings as errors).

[javac] Compiling 10 source files to D:\class
[javac] xxxx\Oracle.java:294: warning: [unchecked] unchecked
[javac] found : java.util.Iterator
[javac] required: java.util.Iterator<java.lang.String>
[javac] Iterator<String> it =
this.hashmap.keySet().iterator();

The Code that generating it is below

Now I've gone out onto the www and a quite extensive search and sort of
getting quite confused about the whole issue. Can somebody shed some
light on what I am missing.
Thanking you in advance for your time and consideration.
You problem is that you are declaring the iterator, 'it', as
having its generic type instantiated to 'String', but 'hashMap'
does not have its generic type constrained at all, so you could
add any type of object in there. So you need to declare it like so:

TreeMap<String> hashMap;

Also, you can then remove the downcast like so:

keys.add(it.next());

Hope that clarifies things!
Tim
 
J

Jeffrey Schwab

Le said:
Hi

I'm relatively new to Java (from a C/C++ background, so please don't
flame me). I having trouble with some warnings like (the code compiles
_but_ I treat warnings as errors).

[javac] Compiling 10 source files to D:\class
[javac] xxxx\Oracle.java:294: warning: [unchecked] unchecked
[javac] found : java.util.Iterator
[javac] required: java.util.Iterator<java.lang.String>
[javac] Iterator<String> it =
this.hashmap.keySet().iterator();

....

TreeMap hashmap;
public ArrayList getSortedKeys()
{
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> it = this.hashmap.keySet().iterator();
while (it.hasNext())
{
keys.add( (String)it.next() );
}
return keys;
}

The compiler can't guarantee that only strings will come from the keySet
iterator, unless of course it can first guarantee that only Strings
will be used as keys. When you create the Map, specify the types of
keys and values you want to allow.

As further niceties:

- You should question the wisdom of calling a TreeMap "hashmap."

- For the benefit of the method's caller, you might also want to
change the compile-time return type of getSortedKeys to ArrayList<String>.

Here's some code:

import java.util.*;
public class Main {
TreeMap<String, Object> map;
public ArrayList<String> getSortedKeys()
{
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> it = this.map.keySet().iterator();
while (it.hasNext())
{
keys.add(it.next());
}
return keys;
}
}
 
L

Le Tubs

Thanks for all your replies, you have cleared up the problems I was
having I'm actually cleaning up code that a DBA wrote ... (I think its
the last time that I "volunteer" for this type of exercise & I won't
bore everyone with a tirade of DBA jokes )
Thanks again for your help it is much appreciated.

David
 
L

Le Tubs

Thanks for all your replies, you have cleared up the problems I was
having I'm actually cleaning up code that a DBA wrote ... (I think its
the last time that I "volunteer" for this type of exercise & I won't
bore everyone with a tirade of DBA jokes )
Thanks again for your help it is much appreciated.

David
 
L

Le Tubs

Thanks for all your replies, you have cleared up the problems I was
having I'm actually cleaning up code that a DBA wrote ... (I think its
the last time that I "volunteer" for this type of exercise & I won't
bore everyone with a tirade of DBA jokes )
Thanks again for your help it is much appreciated.

David
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeffrey Schwab schreef:
Le said:
Hi

I'm relatively new to Java (from a C/C++ background, so please don't
flame me). I having trouble with some warnings like (the code compiles
_but_ I treat warnings as errors).

[javac] Compiling 10 source files to D:\class
[javac] xxxx\Oracle.java:294: warning: [unchecked] unchecked
[javac] found : java.util.Iterator
[javac] required: java.util.Iterator<java.lang.String>
[javac] Iterator<String> it =
this.hashmap.keySet().iterator();

...

TreeMap hashmap;
public ArrayList getSortedKeys()
{
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> it = this.hashmap.keySet().iterator();
while (it.hasNext())
{
keys.add( (String)it.next() );
}
return keys;
}

The compiler can't guarantee that only strings will come from the keySet
iterator, unless of course it can first guarantee that only Strings
will be used as keys. When you create the Map, specify the types of
keys and values you want to allow.

As further niceties:

- You should question the wisdom of calling a TreeMap "hashmap."

- For the benefit of the method's caller, you might also want to
change the compile-time return type of getSortedKeys to ArrayList<String>.

Here's some code:

import java.util.*;
public class Main {
TreeMap<String, Object> map;
public ArrayList<String> getSortedKeys()
{
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> it = this.map.keySet().iterator();
while (it.hasNext())
{
keys.add(it.next());
}
return keys;
}
}

How about

return new ArrayList(this.map.keySet())?

Does all the iterations for you...

H.

- --
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD1hjrjt+nXoeNxg8RAtmaAJ4sY1RAMgsdxy6jG3gN5Qlmy1XnZQCgjlGV
mWc01xSlVR+IvoMSfnK/gdg=
=C9bb
-----END PGP SIGNATURE-----
 
J

Jeffrey Schwab

import java.util.*;
Hendrik said:
How about

return new ArrayList(this.map.keySet())?

Does all the iterations for you...

Oooo... You're good. :)

I think most of us were just concentrating on helping the OP understand
generics. Now I'm kicking myself for not being more clever.
 

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

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top