Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap question

 
Suguna Gollapally
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic