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 / January 2006

Tip: Looking for answers? Try searching our database.

Some help with unchecked types

Thread view: 
Le Tubs - 23 Jan 2006 14:41 GMT
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;
   }
Joe Attardi - 23 Jan 2006 14:48 GMT
>  [javac] found   : java.util.Iterator
>  [javac] required: java.util.Iterator<java.lang.String>
[quoted text clipped - 4 lines]
> 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.
Timbo - 23 Jan 2006 14:49 GMT
> Hi
>
[quoted text clipped - 15 lines]
> 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
Jeffrey Schwab - 23 Jan 2006 15:03 GMT
> Hi
>
[quoted text clipped - 8 lines]
>  [javac]         Iterator<String> it =
> this.hashmap.keySet().iterator();

...

>  TreeMap hashmap;
>  public ArrayList getSortedKeys()
[quoted text clipped - 7 lines]
>         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;
    }
}
Le Tubs - 23 Jan 2006 15:11 GMT
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
Le Tubs - 23 Jan 2006 15:12 GMT
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
Le Tubs - 23 Jan 2006 15:28 GMT
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
Hendrik Maryns - 24 Jan 2006 12:09 GMT
Jeffrey Schwab schreef:
>> Hi
>>
[quoted text clipped - 51 lines]
>     }
> }

How about

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

Does all the iterations for you...

H.

- --
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
Jeffrey Schwab - 24 Jan 2006 12:26 GMT
>>import java.util.*;
>>public class Main {
[quoted text clipped - 10 lines]
>>    }
>>}

> 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.


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.