What is the fastest Java collection for single-threaded functions Contains (Point (x, y))?

In my application, I need to check the collection of 2D coordinates (x, y) to see if there is a given coordinate in the collection, it should be as fast as possible, and only one stream will be available to it. (To check for collision)

Can someone give me a push in the right direction?

+3
source share
5 answers

Absolutely fast, which I can think of, is to maintain a two-dimensional matrix of these points:

//just once
int[][] occurrences = new int[X_MAX][Y_MAX];
for (Point p : points ) {
    occurrences[p.x][p.y]++;
}

//sometime later
if ( occurrences[x][y] != 0 ) {
    //contains Point(x, y)
}

, , boolean. , , , , , Points .

, ( a HashSet ).

Edit

Set<Point>, , . - :

public class PointSet implements Set<Point> {
    private final boolean[][] data; 
    public PointSet(int xSize, int ySize) {
        data = new boolean[xSize][ySize];
    }

    @Override
    public boolean add(Point e) {
         boolean hadIt = data[e.x][e.y];
         data[e.x][e.y] = true;
         return hadIt;
    }

    @Override
    public boolean contains(Object o) {
        Point p = (Point) o;
        return data[p.x][p.y];
    }

    //...other methods of Set<Point>...
}
+5

Trove collections.

int float, long: 32 x- 32 y-cop. TLongHashSet, HashSet, ( java).

int, -

static private long computeKey(int h1, int h2)
{           
    return ((long)h1) << 32 | h2;
}

,

TLongHashSet set = new TLongHashSet()
set.add(long v);
set.addAll(long[] v);
set.containsAll(..);

float, , float long.

+2

HashSet. O (1) . , true O (1), , . , .

+1

? .

Point2D , ? - , , TreeSet, , , B +, , , .

, , SkipList. , O ( ()) ** , , )

0

- , treeet, .

-1

Source: https://habr.com/ru/post/1748891/


All Articles