-1

First of all, I want to describe that there are more than five thousand records. Please let me know the best method for best performance from the following:

String hmVALUE = "";

try
{
hmVALUE = hashmapRECORDS.get(key).toString();
}
catch(Exception ex)
{
hmVALUE = "";
}

// Second method:

String hmVALUE = "";

if(Module.hmQUEUED_REQUESTS.containsKey(key))
{
hmVALUE = hashmapRECORDS.get(key).toString();
}
else
{
hmVALUE = "";
}

I am using try-catch and want to know which method is best.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Principle: never use exceptions for normal control flow if you can get around them (there may be exceptional cases but they are rare). That being said, use the third method: `Object x = hashmapRECORDS.get(key); hmVALUE = x != null ? x.toString() : "";` – Thomas Nov 24 '22 at 07:38
  • Hi and welcome to stack overflow, could you please format your code to make it a bit more readable? – Yonatan Karp-Rudin Nov 24 '22 at 07:38
  • an if statement is not an alternative to a try-catch. an if statement is about a conditional choice, try catch is exception/error handling – Stultuske Nov 24 '22 at 07:39
  • Btw, your second method would not work as your checking for the key in a map different to the one you're trying to get the value from. – Thomas Nov 24 '22 at 07:41
  • lot of thanks, now cleared – user3591863 Nov 24 '22 at 10:25

2 Answers2

1

Neither is ideal. Better to call get() and check if the result is null:

String hmVALUE = "";
Object value = hashmapRECORDS.get(key);
if (value != null) {
    hmVALUE = value.toString();
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
0

Generally speaking, exceptions are much heavier operations for Java to generate (they need to collect stack traces, etc...) so in this specific case, I would say that if-statement is much better than try-catch.

However, for the question in general, those are completely different mechanisms - the 1st one should handle an unexpected error, while the other one is flow control.

Moreover, you can use the short condition ? if true : if false syntax and have the entire code like this:

String hmVALUE = Module.hmQUEUED_REQUESTS.containsKey(key) ? hashmapRECORDS.get(key).toString() : ""

And more specifically in your case, you can just use the getOrDefault() method:

String hmVALUE = hashmapRECORDS.getOrDefault(key, "");
Yonatan Karp-Rudin
  • 1,056
  • 8
  • 24
  • I would not use the `containsKey(key)` + `get(key)` combo as this is doing 2 lookups. If don't really have to know whether the key is in the map, i.e. all you care about is the value, just do `get(key)` and check if you got anything: yes - the key is in the map, no - the key might be in the map or not, in any case there's no value for it. – Thomas Nov 24 '22 at 07:43
  • 1
    I wouldn't use it as well, I just copied the code they wrote, this is why I suggested here using getOrDefault() as it will give the same behavior and be easier to understand – Yonatan Karp-Rudin Nov 24 '22 at 07:45
  • 1
    1) stack traces are only created if/when needed; 2) it actually depends on the data, if we almost always have a hit, the exception is almost not thrown (and *cheaper* than 2 lookup) – user16320675 Nov 24 '22 at 08:07
  • Yeah, that's true, there's no silver built here, and it depends on the case... – Yonatan Karp-Rudin Nov 24 '22 at 08:09
  • @user3591863 can you accept the answer unless you have anything else that is missing for you? – Yonatan Karp-Rudin Nov 24 '22 at 08:32