Save a list of unique Strings in the ArrayList
Why do you need to store it in a List
? Do you actually require the data to be ordered or support index-based look-ups?
I would suggest storing the data in a Set
. If ordering is unimportant you should use HashSet
. However, if you wish to preserve ordering you could use LinkedHashSet
.
If you have a List
containing duplicates, and you want a List
without, you could do:
List<String> newList = new ArrayList<String>(new HashSet<String>(oldList));
That is, wrap the old list into a set to remove duplicates and wrap that set in a list again.