• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

map method containsKey

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But shouldn't that break containsKey as well?
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True - I didn't read that part.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving thread as too difficult for "beginning."
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.

it's also not nice one



thanks
neeraj
 
Evil is afoot. But this tiny ad is just an ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic