Is it faster to add to a collection then sort it, or add to a sorted collection?

TreeSet has a log(n) time complexity guarantuee for add()/remove()/contains() methods.
Sorting an ArrayList takes n*log(n) operations, but add()/get() takes only 1 operation.

So if you’re mainly retrieving, and don’t sort often, ArrayList is the better choice. If you sort often but dont retrieve that much TreeSet would be a better choice.

Leave a Comment