Rien
Rien

Reputation: 1656

What is more efficient? An If Else or a HashMap?

I'm writing some code in Java to check in which quadrant a coordinate is and I was wondering which method is more efficient to check this: a if-else block or the use of a HashMap.

A HashMap would 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)

The 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?

Upvotes: 10

Views: 9702

Answers (7)

Tushar Kurhekar
Tushar Kurhekar

Reputation: 1

Just consider a very basic example of Two Sum. We can solve it with hashmap or simple If-else loops. For sure compiler has to do a lot fewer threads in if-else and consumes less memory. But if we consider large inputs or real-life examples like finding a sum from large datasets, the Iteration of if-else is going to take time and will be complex to write ( as a number of if-else loops will be increased ), Hence we will use Hashing in that case.

Upvotes: 0

Henry Rao
Henry Rao

Reputation: 1

I had the same questions here. and i did some tests.

it was my "universal data convert function", that takes a class and an Object data as inputs, and convert the "data" to the required class.

to handle all "primitive" classes, i have many "if else" statements in the function (like 20 or so).

and one day, i try to use hashmap to store the "class" as keys and the "convert function" as values.

compared the two version, even i used the input which will reach the last of the "if else" statements, the "if else" version is still 4-5 times faster than hashmap (hashmap is initialized at the beginning and cached).

( and actually, when i used the input which will reach only the first "if else" statement and return, the test result is almost the same )

( my env is windows 10 + java 1.8)

Upvotes: 0

ReneS
ReneS

Reputation: 3545

For just four entries? The if-else will be faster. The hashmap has to do more commands to get you there. Fetch hashcode, calculate distance/position, fetch the array entry, and run an equals operation.

Upvotes: 11

Clashsoft
Clashsoft

Reputation: 11882

What you are utilizing in your first example is called Double-Brace Initialization. It creates an anonymous class only for the purpose of laziness, which is extremely inefficient on multiple levels. Also, unless you cache it, a hashmap consumes a lot of memory and has a relatively slow initialization time. A simple if is definitely more efficient here.

Generally, if-else will always be more efficient. But with a certain number of cases, you should use a (properly initialized) Map for the sake of readablity.

Upvotes: 4

tinker
tinker

Reputation: 1406

All these answers are based on difference in if-else and hashmap, but from an algorithm point of view, your if-else will always work more optimally and you don't need all that space in a hashmap. Whenever you get a co-ordinate, if you pass it through the if-else then you will get the answer and that way you don't need to use a map and you can save memory. Make a utility function of the if block for ease of use anywhere in your program.

Upvotes: 0

bhspencer
bhspencer

Reputation: 13570

It depends on how many Coordinates you will have. If you have a very small number then a if else will be slightly quicker than calculating the hash and looking it up. If you are only ever going to have 4 Coordinates then it is likely faster to use if/else than using a HashMap.

Upvotes: 1

Crazyjavahacking
Crazyjavahacking

Reputation: 9707

Most likely HashMap, but it depends on couple of things:

  1. the Coordinate class have to have efficient equals() and hashCode() implementation
  2. whether the Coordinate is immutable and the hash code could be cached
  3. which kind of coordinates you will be passing

Upvotes: 0

Related Questions