File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes map method containsKey Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "map method containsKey" Watch "map method containsKey" New topic
Author

map method containsKey

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hi,

I have a small issue, which I am unable to get -- kinldy provide guidance



while



object is JavaBean in our code


thanks
Neeraj


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40


You have a map, with a key, whose pair has a value of null?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Henry Wong wrote:
You have a map, with a key, whose pair has a value of null?

Henry


Yep, this..


is perfectly legal to do ..

JDBCSupport - An easy to use, light-weight JDBC framework -
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

As per Javadocs
V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

What part you unable to get?



http://muhammadkhojaye.blogspot.com/
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Muhammad: The issue is that the map not only returns null if the key is not in the map, it can also return true if the key is in the map - with the value null.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
thanks a ton for your inputs.

i got it that.it can also return true if the key is in the map - with the value null.

but in our case the value of the object is not null. when I try to iterate through the map using map.values. it returns a non-null collection.

thanks
Neeraj
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Neeraj Vij wrote:
but in our case the value of the object is not null. when I try to iterate through the map using map.values. it returns a non-null collection.


The values() method always return a non-null collection. Or are you saying that it returns a collection that doesn't have a null in it?

Henry
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
yes. it returns a collection that doesn't have a null in it. its an arraylist of size 1. with proper values in that arraylist.

just for my help. can you please clarify. will map.values return a non null collection even if the map size is zero.

thanks
Neeraj.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Neeraj Vij wrote:just for my help. can you please clarify. will map.values return a non null collection even if the map size is zero.

Yes - in this case, it will return a collection of size zero.

Neeraj Vij wrote:i got it that.it can also return true if the key is in the map - with the value null.

This may be a problem with the key. What class is it? Have you defined your own equals() and hashCode() methods for this class? If so, I suggest you show them here. A problem with these can prevent a HashMap from working correctly.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

But shouldn't that break containsKey as well?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
True - I didn't read that part.
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
yes. we have defined equals and hashcode method for the bean class that are put into the hashmap. I was also surprise it was not breaking the conatinskey.

here is the actual code for equals method..it's not a nice one.



thanks for your inputs
neeraj
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Moving thread as too difficult for "beginning."
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
So, is there a hashCode() method to go with the equals()?
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
yes.

it's also not nice one



thanks
neeraj
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: map method containsKey
 
Similar Threads
OO concept order of processing question
JTree with checkboxes not getting updated question
how does == work
Synchronization Problem whil accessing JDBC Code
Hash Code Implementation