More efficient way to remove elements from an array list

前端 未结 3 420
说谎
说谎 2021-01-25 12:45

I have developed a array list something like this

ArrayList list = new ArrayList();
list.add(\"1\");
list.add(\"8\");
list.add(\"8\"         


        
3条回答
  •  旧巷少年郎
    2021-01-25 13:23

    If you just need a set of numbers, then use HashSet and not a List. If you need to preserve the order by which you are putting the numbers, use LinkedHashSet. As for removal, always prefer the version with the iterator, even though in your particular case the performance may be comparable. The idiom with iterator is more widely applicable than indexing, for example if you used a LinkedList, indexing would result in disastrous performance.

提交回复
热议问题