File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "HashMap question" Watch "HashMap question" New topic

HashMap question

Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
You have a HashMap whose keys need to be retrived in a sorted manner
which of the following will be used to achieve this..

1.copy the contents of hashmap into a tree map
2.pass hashmap to Arryas.sort()
3.pass hashmap to collections().sort()
4.Add the kesy of Hashmap to tree set

i guess 1 & 2 options
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Option 1 should work: a TreeMap "class guarantees that the map will be in ascending key order". Option 2 won't work because Arrays.sort() is only defined for array types. Option 3 won't work because Collections.sort() is defined only for things that implement a List interface. Option 4 should also work, because the question implies you're only interested in retrieving the keys (and not the values).

Inquisition: open-source mock exam simulator for SCJP and SCWCD
I agree. Here's the link:
subject: HashMap question
It's not a secret anymore!