• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

map method containsKey

 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But shouldn't that break containsKey as well?
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True - I didn't read that part.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving thread as too difficult for "beginning."
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, is there a hashCode() method to go with the equals()?
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.

it's also not nice one



thanks
neeraj
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic