• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if isEmpty or null

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks that helps me!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
reply
    Bookmark Topic Watch Topic
  • New Topic