Collections.sort uses a modified merge sort with nlog (n) time . If you call the method every time you add, you can get n ^ 2log (n) time. While insertion in TreeSet is guaranteed by log (n). Therefore, if you name it on every addition, it will become n.log (n) . Therefore, I would suggest using TreeSet instead. But TreeSet does not allow duplication, so this may affect your decision.
If you use List, instead of using Collections.sort there is one thing you can do to optimize; as you know, every time you insert an item into the list, the list is already sorted, so using insertion sorting will give you better performance, since insertion sorting works better in such cases.
source share