aspose file tools*
The moose likes Beginning Java and the fly likes if isEmpty or null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if isEmpty or null" Watch "if isEmpty or null" New topic
Author

if isEmpty or null

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Sep 02, 2008
Posts: 87
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).


SCJP 5.0, SCJD in progress
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Jun 28, 2008
Posts: 580
thanks that helps me!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if isEmpty or null