5

I have a HashMap<String, String>. If I want to create string array of hashmap.values(), we can create it as

String[] strArray = new String[hashmap.size()]

But my problem is if hashmap values contain "A,B,C" then I need to add A and B and C to strArray.

2
  • why not first count the number of needed items (traversing the hashmap once), and than actually create the array and put the values into it? Commented Jun 21, 2012 at 12:27
  • I guess noone will write you an email, the answers will come in here... ;) Also, can you tell us, what you are trying to do with this array? If this needs to dynamically change, you can just use one of the available collections (like ArrayList<String>). Commented Jun 21, 2012 at 12:27

3 Answers 3

21

Use an ArrayList.

List<String> myList = new ArrayList<String>();

No matter what the size is of your HashMap you can easily work with your ArrayList.

If you need an array, you can use

String[] arr = myList.toArray(new String[myList.size()]);

when you have finished.

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

4 Comments

+1 If you need an array, you can use String[] arr = myList.toArray(new String[myList.size()]) when you have finished.
@PeterLawrey edit my answer and add this point as well please. :-)
I think you are not able to understand my question. It may be becos i may not put the question in rite words. Any Way i ll exxplain it again. For Ex: HashMap Values are
Thanks Peter.. I think you are not able to understand my question. It may be becos i would not put the question in rite words i think. Any Way i ll exxplain it again. For Ex: Map<String, String> map = new HashMap <String, String>(); map.put("1", "A,B,C"); map.put("2", "P,Q,R...,Z"); So i need to create the string Array with values (A,B,C,P,Q,R...,Z). Note that the comma separated values in Map may spread upto any length(wic is dynamic). Please give me the suggestions.
2

You can take a copy of the values whenever you need an array.

Map<Double, String> map = ...
String[] values = map.values().toArray(new String[map.size()]);

If you change the map (even if the size doesn't change), the array won't change and you need to take another copy. Do the values need to be unique?

So i need to create the string Array with values (A,B,C,P,Q,R...,Z).

In that case it appears you want to do the following.

Map<Double, String> map = ...
List<String> valueList = new ArrayList<>();
for(String value: map.values())
   valueList.addAll(Arrays.asList(value.split(",")));
String[] values = valueList.toArray(new String[valueList.size()]);

4 Comments

Thanks Peter.. I think you are not able to understand my question. It may be becos i would not put the question in rite words i think. Any Way i ll exxplain it again. For Ex: Map<String, String> map = new HashMap <String, String>(); map.put("1", "A,B,C"); map.put("2", "P,Q,R...,Z"); So i need to create the string Array with values (A,B,C,P,Q,R...,Z). Note that the comma separated values in Map may spread upto any length(wic is dynamic). Please give me the suggestions.
Do you want an element in the array for every String between the commas? If so, I have added a solution for that.
Yes Peter Lawrey. I want an element in the array for every String between the commas. To Build String Array I need knw the SIZE(dynamic). Im not able to find the SIZE.
You don't need to know the size in advance, Using a List first you can determine the size before creating the array (as it does in the example)
0

Does it need to be an array? With Maps you can get the key of value sets.

2 Comments

Thanks for responding John. yes it needs to be array. And the values in the HashMap is comma separated. Those comma seperated values are splitted by comma and added to String Array Separately.
You could mimic ArrayList set a default initial size and if it is exceeded use the array copy in system (I think) to copy the values to a larger array. Its not the most effecient but you seem to need an array

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.