I'm trying to implement a method of searching trough a large amount of 2D points for those that match a certain range. I'm thinking of creating HashMaps for `<X, Point>` and `<Y, Point>` but I'm wondering is HashMap good for doing this, because I will be taking points in a range, based on values from x\_min to x\_max and y\_min to y\_max.

So I will basically take all points from `<X,Point>` searching from x\_min to x\_max and compare them to points taken from `<Y,Point>` from y\_min to y\_max...

```
HashMap<Integer,Point> x_coordinates = new HashMap<Integer,Point>();
for(int i=x_min;i<=x_max;i++){
    if(x_coordinates.containsKey(i))
        x_coordinates.get(i);
}

HashMap<Integer,Point> y_coordinates = new HashMap<Integer,Point>();
for(int i=y_min;i<=y_max;i++){
    if(y_coordinates.containsKey(i))
         y_coordinates.get(i);
}

```

Is there a faster way to get a range of values from a HashMap, or some other type of data structure?

For example, if range = 200, select all points that match the range from x-200 to x+200 and from y-200 to y+200

I'm trying to avoid iteration through all of the points, but also I want to avoid making a huge 2D matrix, because the points in question are scattered around a large area.

---

After some testing and based on MichaelT's answer, I've written this code:

```
TreeMap<Integer, TreeMap<Integer, Integer>> values;
for (int x : values.subMap(x_min, x_max).keySet()) {
        for (int y : values.get(x).subMap(y_min, y_max).values()) {
            // y here, represents the values of points in range...
        }
    }

```