I would like to know if there is any api available for Java which is just like heapq in Python.
Asked
Active
Viewed 2,470 times
1 Answers
2
Have you looked at java.util.PriorityQueue
?

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
Actually i wanted to do this in java.http://stackoverflow.com/questions/1034846/finding-nth-item-of-unsorted-list-without-sorting-the-list/1036240#1036240 – Emil Sep 17 '10 at 10:27
-
@Emil: Okay, so what's stopping you? It's probably easiest to specify a reversing comparator, and then just use the iterator from the head of the queue. – Jon Skeet Sep 17 '10 at 10:34
-
Is this the best way to find the k'th smallest element?I found selection algorithms with complexity O(n) as the answer to the question.But couldn't find any implementation in Java. – Emil Sep 17 '10 at 10:44
-
@Emil: No, I doubt that it's the best way to find the kth smallest element, as it's basically sorting the whole thing. Just take the selection algorithms you've found and port them to Java - it shouldn't be too hard. – Jon Skeet Sep 17 '10 at 10:50