Converting a TreeSet to ArrayList?

一笑奈何 提交于 2019-12-29 06:41:10

问题


I have a TreeSet which contains > 100k objects. I have another method which requires ArrayList as an param.

Is there any way I can accomplish this without iterating whole TreeSet and then adding each object manually to ArrayList ?


回答1:


How about this:

new ArrayList<T>(set);



回答2:


ArrayList has a convenience method addAll that fits the bill nicely:

final Set<Object> set = ...
List<Object> list = new ArrayList<Object>(someBigNum);
list.addAll(set);


来源:https://stackoverflow.com/questions/9322405/converting-a-treeset-to-arraylist

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