0

I've been searching around for this but I can't seem to find a solution that works. I currently have a hashmap

 HashMap<String,Float> map = new HashMap<String,Float>();

Now I want to access just the key part of the hashmap which is a column of strings. And I would like to create a new array of Strings from those values. If I were to just convert them:

map.getKey().toArray();

That would give the float values too in it's array.. So how do I do this?

3 Answers 3

7
map.keySet().toArray(new String[0]);

See http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html#keySet%28%29.

Sign up to request clarification or add additional context in comments.

1 Comment

Now that's an answer I can +1. ;)
2
map.keySet().toArray(new String[map.size()]);

will give you a string array with the keys in the map. Casting map.keySet().toArray() to String[] will not work because it returns an array of type Object.

Comments

0

I don't know what a "column of strings" is, but what you're looking for is this :

String[] key_array = (String[])map.keySet().toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.