The total time complexity I think should be O(N+k+(N-k)lgk), where N is the time spent on traversing over the map, k is the time to build heap, and (N-k)lgk is the time to insert the remaining (N-k) elements.

@codingcoconut In java 8 we have something called as Functional Interfaces (an interface with only one abstract method). Now using lambdas, we can implement the method of that functional interface as shown above. It is a very cool addition to Java 8.

I dont think you reached (N-k)lgk. Though you maintained the size of que to be k, you still enque N elements which typically takes logk time. so, I believe it is nlgk for the below for loop. More over, you do the poll() when the size is larger. Actually, we only need to poll() if the peek() value is larger than the current value. That will save some time too.