How to return the k-th element in TreeSet in Java?

后端 未结 8 2003
-上瘾入骨i
-上瘾入骨i 2020-12-30 01:07

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?

8条回答
  •  爱一瞬间的悲伤
    2020-12-30 01:25

    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.

提交回复
热议问题