aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Danchisholms: Collections 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 "Danchisholms: Collections question" Watch "Danchisholms: Collections question" New topic
Author

Danchisholms: Collections question

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Arrow Marked options are correct! Please eleborate the correct options.
My main concern is over constant time performance.

Modified post:
Sorry A,F,H are the correct answer!

Regards,
cmbhatt
[ April 17, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you sure that e and h are correct?
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
I think this is becuase the Hashtable,HashMap and LinkedHashMap all have one thing common.

The hashCode and equals functions are used to find a key.Given a key value the time taken to compute the hashCode would be the same for any key value.

In case of add, it computes the hashCode of the key to be added and lands up in a say bucket, does equals comparision with the keys in the bucket , replaces oldkey's value with newkey's value the same key is found and adds key if no key is found..

In case of contains it computes the hashCode of the key to be searched and lands up in a bucket, does equals comparision with the keys in the bucket and the input key and returns true or false.

In case of remove too, it computes the hashCode of the key to be searched and lands up in a bucket, does equals comparision with the keys in the bucket and the input key and removes the key value if found.

So all in all the hash computing time is constant, landing up in a bucket time is constant, the doing equals comparision within a bucket might vary depending on how efficient the hash algorithm is but generally
the time for all this operations can be considered constant.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,


how I understand this question:

first feature (key/value pairs)
The only collections that provide keys are maps. Maps are collections that have "map" in their name. Hashtable is also a map, but has no "map" in its name because of historical reasons.
So we can strike out all wrong answers and only
LinkedHashMap
TreeMap
HashMap
Hashtable (old synchronized version of HashMap)
remain.


second feature (Duplicate entries replace old)
true for all maps, cause duplicate keys "override" old key value pairs.


third feature (constant-time performance for add...)
TreeMap is ordered. Every time you add something, the tree has to be ordered again. And the time to do this depends if you add in the middle or at the tail. IE it depends if you add an "N" or a "Z", simply speaking.
Re-Ordering also takes place when you remove(), so we can strike TreeMap out two times.
LinkedHashMap
HashMap
Hashtable
remain.


About LinkedHashMap:
It is not ordered, just sorted (by entry time). Every time you add a pair it is just glued to the tail, should be time constant.



Bu.


all events occur in real time
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks a lot Burkhard,

Ramification of answering was great. I got it step by step as you described.
It can help me to find answers of the similar questions.


For you:

Regards,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Danchisholms: Collections question