Answer the question
In order to leave comments, you need to log in
How to speed up filling HashMap?
I have a coordinate system (X,Y axes). I am given 4 numbers: x1,x2,y1,y2. These numbers are the vertices of a square -
the top right is x1,y1 and the bottom left is x2,y2.
You need to fill any array or collection with a matrix with 1 and 0. Where 1 is a square at this point, 0 is empty. Below is an example:
000000000
111111000
111111000
111111000
000000000
private Map<Pair<Integer, Integer>, Integer> calculateRectangleSquare(int x1, int x2, int y1, int y2) {
int minY = Math.min(y1, y2);
int minX = Math.min(x1, x2);
int maxY = Math.max(y1, y2);
int maxX = Math.max(x1, x2);
Map<Pair<Integer, Integer>, Integer> rectSquare = new HashMap<Pair<Integer, Integer>,Integer>();
for (int i = minX; i < maxX; i++) {
for (int j = minY; j < maxY; j++) {
Pair<Integer, Integer> key = new Pair<Integer, Integer>(i, j);
rectSquare.put(key, true);
}
}
return rectSquare;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question