title: Java: Get values in a range from a collection
body: <p>Suppose I have a (sorted) collection, be that a List, a Map, a Set or anything else. What would be the best solution to get all the values within a certain range.</p>
  
  <p>For instance I have a list of Integers like so: [1, 5, 7, 9, 12, 30, 50, 100]</p>
  
  <p>I would like to retrieve the 8 +- 5 values, which is going to be: [5, 7, 9, 12]</p>
  
  <p>I know about the NavigableMap which is quite interesting, however I can only retrieve one element using it. </p>
  
  <p>Do you have any tips on an algorithm that has a better complexity than O(N), maybe O(NLogN) or a specific collection I could use?</p>
  
  <p>Thanks very much!
  Costi</p>