aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes danchisholm.net HashMap Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "danchisholm.net HashMap Question" Watch "danchisholm.net HashMap Question" New topic
Author

danchisholm.net HashMap Question

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


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


cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I'm not sure what your question is.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 15, 2007
Posts: 557
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.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 15, 2007
Posts: 557
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

Joined: Feb 15, 2007
Posts: 557
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 15, 2007
Posts: 557
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

Joined: Apr 06, 2007
Posts: 4
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

Joined: Jan 05, 2007
Posts: 98
HashMap is unordered Map its allow nulls for keys and values ( as i remeber at least )
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: danchisholm.net HashMap Question