How do I get a String[] from a Map<String,Object> keys ?

J

Jerome Eteve

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

Steve W. Jackson

Jerome Eteve said:
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.

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

kaldrenon

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

kaldrenon

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

Steve W. Jackson

kaldrenon said:
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.

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

kaldrenon

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[])
 
R

Roedy Green

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.

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()]) ;
 
R

Roedy Green

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

Roedy Green

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.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top