askaka12
askaka12

Reputation: 205

Using keySet() method then changing the Set to a String Array? Java

So this should be really simple since I know it's possible (I just don't understand 'Set' very much).

So basically there is this TreeMap, let's call it aTree. So I need to do something like:

somethingHereProbably = aTree.keySet();
somethingHereProbably.toStringArray();

Upvotes: 10

Views: 34788

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

You can do

Map<String, Object> map = ...
String[] strings = map.keySet().toArray(new String[map.size()]);

This works for any kind of map, including TreeMap

Upvotes: 63

Related Questions