简体   繁体   中英

Convert HashMap.keySet() to CharSequence[]?

I have a method in a client class that gathers data from a server, and returns a HashMap. I want to list the keys in an android dialog. The android API requires that the data must be in in the form of a CharSequence[]. So far as I can tell, there is no direct way to convert these data types. What would be the best way to accomplish this?

Here is the declaration and initialization of the hashmap and keyset:

Map <String, String> players = client.listPlayers();
Set<String> player_names = players.keySet();

And here is the link to the android Dialog documentation.

Note: CharSequence, which is able to be casted from a Set is not the same as CharSequence[]

Any help is appreciated.

-Sean W.

CharSequence[] player_names = players.keySet().toArray(new CharSequence[0]);

This tells the method the type of the array without allocating any space for elements. It will allocate a correctly-sized array for you.

If you prefer to allocate the real array yourself:

CharSequence[] player_names = players.keySet().toArray(new CharSequence[players.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM