Range intersection is a simple, but non-trivial problem.
Its has been answered twice already:
Sounds like you need a class that implements the SortedSet interface. TreeSet is the implementation that ships with the core API.
Have one set holding the ranges sorted by lowest value, and one sorted by highest value.
You can then implement the equivalent of the database algorithm using the in-memory sets.
As for whether this is actually faster than O(n), I couldn't say.