converting hashmap to stringarray

若如初见. 提交于 2019-12-04 17:19:16

drinkar.keySet().toArray() returns Object[] not String[]

One of the way may be:

user Collections.toArray(StringArry)

(or)

Iterate through the keySet and add each element to array.

Use toArray(T[]) as:

String[] list = drinkar.keySet().toArray(new String[0]);

By giving an empty array as the argument, you tell toArray to create a new array of the same type for you that will be just of the correct size.


Just a note: If you can choose, it's usually more convenient (and safer) to work with collections such as ArrayList instead of arrays.

String st[]=hm.keySet().toArray(new String[hm.size()]);

Another option is create ArrayList<type> and add elements and return list.toArray(). This is exactly what toArray does on entryset Can not type code since not front of desktop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!