I have an ArrayList.
How can I instantiate a new List with the same data but sorted?
I thought about the following:
- Use the
ArrayListcopy constructor and then useCollections.sort - Use a
TreeSet
For option (1) there is the extra overhead of copying the elements and then sorting.
For option (2) duplicates will be removed.
What is the best way for this?
The “best way” depends on your requirements: do you want the duplicates removed? Use a
TreeSet; do you want to keep the duplicates? Copy, then sort. Trying to get the fastest one out of the two is premature optimization.