Skip to content

Tag: binary-search

How to iterate from a given key in Java TreeMap

I have a TreeMap, and want to get a set of K smallest keys (entries) larger than a given value. I know we can use higherKey(givenValue) get the one key, but then how should I iterate from there? One possible way is to get a tailMap from the smallest key larger than the given value, but that’s an overkil…

First occurrence in a binary search

I’m tinkering with some code and I realized something I never knew. A normal binary search will return a random index in a data set for a key that occurs more than once. How can I modify this code below to return the first occurrence? Is this something people do? Answer Having found a matching value, yo…