Wednesday, June 4, 2008

Java : HashMap to StringArray

This below code helps you to Convert the HashMap Values / Keys to StringArray, This will make your code more compact.

import java.util.HashMap;

public static void main(String str[])
{
HashMap hMap = new HashMap();
hMap.put("1","One");
hMap.put("2","Two");

String strArray[] = null;

// converting the Values's to String Array
strArray = (String[])(hMap.values().toArray(new String[0]));
System.out.println( strArray[0] + strArray[1]);

// converting the Key's to String Array
strArray = (String[])(hMap.keySet().toArray(new String[0]));
System.out.println( strArray[0] + strArray[1]);
}

No comments: