• Post Reply Bookmark Topic Watch Topic
  • New Topic

Map instance Keys and Values  RSS feed

 
Ben Phoebi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a Map instance contain keys that dont map to any values and or values that dont map to any keys?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Phoebi wrote:Can a Map instance contain keys that dont map to any values and or values that dont map to any keys?

Some Map implementations support null values and some also support a single null key so using one of these implementations you could add keys with a null value and at most one value with a null key.
 
Ben Phoebi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is it possible to remove a key from a Map implementation's instance without removing the value it maps to?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Phoebi wrote: So is it possible to remove a key from a Map implementation's instance without removing the value it maps to?

No, each key maps to a single value. Removing the key will remove the value. You can't have keys without any associated value or visa versa.

The point I was making in my last post was for some implementations of Map you can use null instead of an object because I thought that was what your question was asking - sorry if I misunderstood your question.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Phoebi wrote: So is it possible to remove a key from a Map implementation's instance without removing the value it maps to?

Nope. But remove() returns the value associated with the key you just removed, so you could always add it back.

I guess the question is: Why do you want to do this? Or possibly: Why do you think you might want to do this?

Winston
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Phoebi wrote:Can a Map instance contain keys that dont map to any values and or values that dont map to any keys?

What output come when you try with a simple code? You can also study different implementation of Map including WeakHashMap.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Khojaye wrote:What output come when you try with a simple code? You can also study different implementation of Map including WeakHashMap.

You first statement is fine, but let's not blind Ben with irrelevancies here, Muhammad. WeakHashMap has absolutely nothing to do with the question he asked; I think you're extrapolating what his problem might be.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!