Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

danchisholm.net HashMap Question

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Source: Source Of The Question

The answer is "d"
I have doubt regarding second bullet:
"Allows null elements, keys, and values."

As I know HashMap allows only one null key and multiple null values.
Please guide...

Thanks and Regards,
cmbhatt
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your question is.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap allows only one null key and multiple null values.
The second bullet of the question says:

"Allows null elements, keys, and values."
How can it be true?
"Keys"? (null "key" is true but how can null "keys" be true).


Thanks and Regards,
cmbhatt
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

You are correct.
Given> Allows null elements, keys, and values
//The above statement can act like "overlooked trap"

Allows null Keys is a real trap there.
Thus option d is wrong.

It should be g.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srini,

Isn't concern of the question something different? But as per
the specification we are correct.

One more doubt,
Is accessing an element using HashSet is fast in comparison to
TreeSet?


Thanks and Regards,
cmbhatt
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think they meant to imply multiple keys and values.

They were just saying that null could be used as a key or value(s).
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
For HashSet -- Any Unique elements only allowed...
One Element per Bucket.
1.1 just calculating hashcode() takes negligible time.
1.2 jumps to bucket fetches element.

For TreeSet -- it has to execute compareTo() on each node from root node to reach leaf nodes(worst case) down the tree.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

The Thing is we have to be consider allows null keys because...
(g). None of the above option is given.

If (g) is not given then we can assume that thats a typo.

Hope its clear now. Why we are forced to select(g).
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think its just a confusion on wording.

When it says null elements, keys and values, the word elements here is a general term. In this case elements means keys and values.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry,

we can't accept ambigious results.

On exam we will even have question Ambigious situations.

ex on threads like "which is guarenteed behavior".

There is no scope for Ambiguity in questions or answers.
 
n singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi chandra,
as you say
----------------------------------------------------
The answer is "d"
I have doubt regarding second bullet:
"Allows null elements, keys, and values."

As I know HashMap allows only one null key and multiple null values.
-----------------------------------------------------------------------

answer d is right.

at runtime HashMap overwrite the null key.
so you have only one entry for null key.
 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap is unordered Map its allow nulls for keys and values ( as i remeber at least )
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take a look at what the HashMap API says:
This implementation ... permits null values and the null key.


So, strictly speaking, only one element can be (null reference, reference to anything). The other elements must be like (non null reference, reference to anything).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic