Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert Map Values into String Array

Tags:

java

android

Here ,I am trying to convert map values into String array but i am getting

Error

ERROR/AndroidRuntime(23588): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;

Code

Map<String,String> contactNumber = new HashMap<String,String>(); 

String results [] =  (String[]) contactNumber.values().toArray();
like image 950
Hunt Avatar asked Feb 17 '12 13:02

Hunt


People also ask

How do you convert a map to a String?

Use Object#toString() . String string = map. toString();

How do you convert the values of a HashMap to an array?

One way to convert is to use the constructor of the ArrayList. In order to do this, we can use the keySet() method present in the HashMap. This method returns the set containing all the keys of the hashmap.


2 Answers

You should use the other toArray(T[] a) method.

String[] result = contactNumber.values().toArray(new String[0]);
like image 167
user802421 Avatar answered Oct 19 '22 06:10

user802421


You can't perform the cast like this. Instead, call the other toArray method:

String[] result = contactNumber.values().toArray(new String[0]);
like image 25
MByD Avatar answered Oct 19 '22 04:10

MByD