Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the k-th smallest element. Can TreeSet in Java do this?
I know this question is quite old, but since TreeSet implements NavigableSet you have access to the subSet method which runs in constant time.
subSet(k, k + 1).first();
The first() call takes log(n) time where n is the size of the original set. This does create some unnecessary objects which could be avoided with a more robust implementation of TreeSet, but it avoids using a third party library.