I am writing Java code to check which quadrant has a coordinate, and I was wondering which method is more efficient to check: if-else block or using HashMap.
HashMap will look like this:
private static final Map<Coordinate,Quadrant> quadMap = new HashMap<Coordinate, Quadrant>(){{ put(new Coordinate(0,0), Quadrant.Q1); put(new Coordinate(0, 1), Quadrant.Q2); put(new Coordinate(1, 0), Quadrant.Q3); put(new Coordinate(1, 1), Quadrant.Q4); }};
And then where I want to get my quadrant:
return quadMap.get(coordinate)
If-else implementation:
if (x < 1){ if (y < 1){ return Quadrant.Q1; } else { return Quadrant.Q2; } } else { if (y < 1){ return Quadrant.Q3; } else { return Quadrant.Q4; } }
Or is there another, more efficient way to do this?
source share