I read data from a text file, so there may be:
John Mary John Leeds
I now need to get 3 unique elements in the ArrayList, because there are only 3 unique values in the file output (as above).
I can use a HashTable and add information to it, then simply copy its data into the List.
Are there other solutions?
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 useHashSet. However, if you wish to preserve ordering you could useLinkedHashSet.