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 / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Jerome Eteve - 02 Jul 2007 15:05 GMT
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.
Steve W. Jackson - 02 Jul 2007 15:10 GMT
> 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

kaldrenon - 02 Jul 2007 15:17 GMT
>     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
Roedy Green - 02 Jul 2007 21:07 GMT
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


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



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