Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if isEmpty or null

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Map:

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

Is there a difference of

statement 1:



and

statement 2:



Which one should I prefer (the shorter one statement2?)

I cannot find any differences.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statements are different! The first one could lead to a NullPointerException in the case that the map is not empty but the requested key not contained. In the case where there is a value for the provided key, the isEmpty() method is called for the contained list.
Your second statement simply checks whether the provided key is used in the map (alternatively, there is a method containsKey() for this kind of task).
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should consider the four different possible conditions that you may need to check on (based on your first if statement):

A) The Map is completely empty
B) The Map is not empty, but doesn't contain the requested key
C) The Map is not empty, but the List value for the key is empty
D) The Map is not empty, and the List value for the key is not empty

Your 2 statements will provide the same results for 2 of these conditions, but different results for the other 2 conditions.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:
You should consider the four different possible conditions that you may need to check on (based on your first if statement):

A) The Map is completely empty
B) The Map is not empty, but doesn't contain the requested key
C) The Map is not empty, but the List value for the key is empty
D) The Map is not empty, and the List value for the key is not empty

Your 2 statements will provide the same results for 2 of these conditions, but different results for the other 2 conditions.


There is also

E) The Map is not empty, and the List value for the key is null
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this should be handle all cases?

I want to express all possibilities in one if clause, instead of using



So I make use of the | and &, and || and &&:



Is this not enough?
[ October 29, 2008: Message edited by: nimo frey ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using | or & rather than || and && might cause you problems.

[diversion] If is often bad practice to use null checks like that; there was discussion about a week ago about it. Better to make sure the Map has been instantiated in your constructor.[/diversion]

But if your Map is in fact null, then using | or & will mean the next test is performed, leading to a NullPointerException.
If you use && and the Map is in fact null, the JVM will assume the whole statement evaluates to false, and not carry out the second check, so your application will continue to run.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks that helps me!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic