How to return the kth element in TreeSet in Java
问题 Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the kth smallest element. Can TreeSet in java do this? There seems no built-in method of TreeSet to do this. Please help me. 回答1: I don't believe that TreeSet has a method that directly does this. There are binary search trees that do support O(log n) random access (they are sometimes called order statistic trees ), and there are Java implementations of this data structure available. These