Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Sorted keys from HashMap

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To retrieve the keys of a HashMap in sorted order,
1.Copy the contents of HashMap to TreeMap.
2.Add the keys the keys of HashMap to TreeSet
3.Pass the HashMap to Arrays.sort
4.Pass the HashMap to Collections.sort

This question is in Whizlabs Diagnostic test.

Can anyone give the answer with explanation
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To retrieve the keys of a HashMap in sorted order,
1.Copy the contents of HashMap to TreeMap.
2.Add the keys the keys of HashMap to TreeSet
3.Pass the HashMap to Arrays.sort
4.Pass the HashMap to Collections.sort



1. false -> Copying the contents to TreeMap will sort the Map as per the values and not as per the keys
2. true -> When something is added to TreeSet, it is put in ordered manner. So, when keys are put into it, they are ordered
3. false -> This is used to sort the arrays not Map
4. false -> This is used to sort collection (ArrayList, Vector,etc) not Map
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ashish
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. false -> Copying the contents to TreeMap will sort the Map as per the values and not as per the keys



Thats incorrect. From the documentation of the TreeMap class:

Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic