I need to convert a HashMap
to an array; could anyone show me how it\'s done?
@SuppressWarnings("unchecked")
public static E[] hashMapKeysToArray(HashMap map)
{
int s;
if(map == null || (s = map.size())<1)
return null;
E[] temp;
E typeHelper;
try
{
Iterator> iterator = map.entrySet().iterator();
Entry iK = iterator.next();
typeHelper = iK.getKey();
Object o = Array.newInstance(typeHelper.getClass(), s);
temp = (E[]) o;
int index = 0;
for (Map.Entry mapEntry : map.entrySet())
{
temp[index++] = mapEntry.getKey();
}
}
catch (Exception e)
{
return null;
}
return temp;
}
//--------------------------------------------------------
@SuppressWarnings("unchecked")
public static T[] hashMapValuesToArray(HashMap map)
{
int s;
if(map == null || (s = map.size())<1)
return null;
T[] temp;
T typeHelper;
try
{
Iterator> iterator = map.entrySet().iterator();
Entry iK = iterator.next();
typeHelper = iK.getValue();
Object o = Array.newInstance(typeHelper.getClass(), s);
temp = (T[]) o;
int index = 0;
for (Map.Entry mapEntry : map.entrySet())
{
temp[index++] = mapEntry.getValue();
}
}
catch (Exception e)
{return null;}
return temp;
}