I have developed a array list something like this
ArrayList list = new ArrayList();
list.add(\"1\");
list.add(\"8\");
list.add(\"8\"
If you need to store the duplicates I suggest you to use Map<Integer,Integer>
where the 1st Integer is the key and the 2nd is the number of key occurences . So when you add the key that already exists you just increment the corresponding counter. (In remove operation you doing vice - versa). When you need all the distinct values, you just use Map.keySet().
Performance wise they should be similar. Have you tested? If you want to use the built in methods, you can do this with a similar performance.(to be confirmed by testing):
list.removeAll(Arrays.asList("8"));
Finally if you want a list without duplicates, use a Set as others have mentioned.
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.