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