I all.
I have a
Map<String,Float> foo = ....
And the following line of code a couple of instructions after:
String[] fields = (String[])(foo.keySet().toArray()) ;
But at the runtime, I have a class cast exception telling me that it's
not possible to cast an Object down to a String.
How can I do to have an array of strings from the foo map key set ?
Thanks for your help.
> I all.
> I have a
[quoted text clipped - 11 lines]
>
> Thanks for your help.
Revisit the API and you'll find that the Set method toArray() explicitly
returns an Object array. But there's an alternative method which
returns an array of the type used in the Set. Try that one.

Signature
Steve W. Jackson
Montgomery, Alabama
> Map<String,Float> foo = ....
>
> And the following line of code a couple of instructions after:
>
> String[] fields = (String[])(foo.keySet().toArray()) ;
Would it be possible to do this:
String[] fields = ( Set<String> foo.keySet() ).toArray();
instead? I don't know, myself, but by making the set generic I think
you would make the resulting array fit that type.
kaldrenon - 02 Jul 2007 15:19 GMT
> String[] fields = ( Set<String> foo.keySet() ).toArray();
I meant to say ( (Set<String>) foo.keySet() ).toArray();
without the () around the generic it's not a cast.
Steve W. Jackson - 02 Jul 2007 17:49 GMT
> > Map<String,Float> foo = ....
> >
[quoted text clipped - 8 lines]
> instead? I don't know, myself, but by making the set generic I think
> you would make the resulting array fit that type.
That Set is already generic by virtue of the Map being so. In keeping
with my earlier reply, the solution is to use the *other* toArray method
present on the Set interface (since keySet returns a Set) -- the one
that lets you specify an existing or empty array of the appropriate type.

Signature
Steve W. Jackson
Montgomery, Alabama
kaldrenon - 02 Jul 2007 18:15 GMT
On Jul 2, 12:49 pm, "Steve W. Jackson" <stevewjack...@knology.net>
wrote:
> the solution is to use the *other* toArray method
> present on the Set interface (since keySet returns a Set) -- the one
> that lets you specify an existing or empty array of the appropriate type.
Which can be used thus [1]:
Suppose x is a set known to contain only strings. The following code
can be used to dump the set into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().
[1] taken from http://java.sun.com/javase/6/docs/api/java/util/Set.html#toArray(T[])
Roedy Green - 03 Jul 2007 14:23 GMT
> String[] y = x.toArray(new String[0]);
You might as well create the array the proper size to save the
overhead of recreating it the proper size with the juggernaut of
reflection.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 03 Jul 2007 14:26 GMT
>Would it be possible to do this:
>
>String[] fields = ( Set<String> foo.keySet() ).toArray();
If you try the experiments you soon discover how brain damaged toArray
and generics are for this task. The practical answer is, don't try to
mix generics and arrays. You can do with iterators, and Iteratables
pretty well all you needed arrays for.
see http://mindprod.com/jgloss/hashmap.html
for alternative code.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
On Mon, 02 Jul 2007 14:05:27 -0000, Jerome Eteve
<jerome.eteve@gmail.com> wrote, quoted or indirectly quoted someone
who said :
> And the following line of code a couple of instructions after:
>
[quoted text clipped - 6 lines]
>
>Thanks for your help.
There are two versions of toArray. You used the lame one that returns
Object[] Always us the one where you provide an empty typed array.
Object[] toArray()
Returns an array containing all of the elements in this set.
<T> T[]
toArray(T[] a)
Returns an array containing all of the elements in this set;
the runtime type of the returned array is that of the specified array.
So you would write:
Set<String> keys = foo.keySet();
String[] fields =keys.toArray( new String[ keys.size()]) ;
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com