0

I am using a

set < string > queue = new set < string > ();

I am using an if to filter out null values

if(queue.size() > 0 && !queue.isEmpty())

{ system.debug('queue '+queueaddress); }

In logs I can see that queue is {null} But still it is going inside the if condition . Please advise how to do a null check here

Thanks in advance

3
  • Both the IF conditions return false, so there is no way that execution would enter the IF loop.
    – arut
    Jan 19, 2021 at 7:42
  • Are you trying to avoid adding a null entry to the set, or to detect that the set contains a null value? Or are you trying to detect a null set?
    – Phil W
    Jan 19, 2021 at 7:42
  • I think your queue Set might be containing null value. The only way to avoid it to iterate and skip the null value from Set. Or best is to not add null to set. Jan 19, 2021 at 7:56

1 Answer 1

1

The only way it will go inside if condition is when your Set has null as a value inside it.

The only way to avoid it to remove it via queue.remove(null); or iterate and skip the null value from Set Or best is to not add null to set.

4
  • I prefer removing null afterwards instead of an if statement inside the loop, because you only end up running the remove(null) once instead of hundreds or thousands of if statements being executed.
    – sfdcfox
    Jan 19, 2021 at 11:31
  • Thanks @sfdcfox for your inputs, I prefer to not add nulls by checking it earlier. Jan 19, 2021 at 12:00
  • That's fine. It's only ~10% more CPU time to do it that way. Just saying that if you need performance gains, this is an easy one to utilize.
    – sfdcfox
    Jan 19, 2021 at 12:23
  • Thanks, as usual, for your valuable inputs @sfdcfox Jan 19, 2021 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .