• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Is there a reason why TreeMap & TreeSet is used in this rather than HashMap & HashSet

 
Ranch Hand
Posts: 327
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I struggled on trying to finish this exercise and now I'm studying the suggested solution.  The suggested solution uses TreeMap & TreeSet which was never brought up in any previous assignments so I didn't think of it myself.  However when I changed TreeMap to HashMap ( just to study and understand ), then the TreeSet to HashSet it didn't change the outcome.

Googling TreeSet vs. HashSet says HashSet is preferred because it's faster.
https://stackoverflow.com/questions/1463284/hashset-vs-treeset

But between HashMap and TreeMap , the TreeMap guarantees that the order of the keys is sorted.  So Maybe that's why it's being used?  However the Person class implements Comparable so doesn't that take care of the sort issue?

Thank You for any feedback and discussion.

Screen shot of the assignment
https://imgur.com/a/aTp8pVq


















 
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would not see any difference doing a search. Where you would see a difference is if you were iterating through the Map. In the case of a HashMap the Entries would be in the order of their hash values which, from a user's perspective, would seem to be random. A TreeMap keeps the Entries in sorted order so that iterating through them would appear to make some sense to the user. If you were to implement a command to print the entire Map then a TreeMap would be useful. On the other hand, if you're just going  to search then HashMap would be faster.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The TreeMap for the commands ensures that, when the printInstructions method is called, the list of instructions appear in the correct order.
Using a HashMap and it's unlikely they would appear correctly.
 
Lisa Austin
Ranch Hand
Posts: 327
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey Brown and Dave Tolls.
I thought I responded to this post earlier but I've been kicked out frequently by this website so I guess my response was lost.
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about being kicked out, we have been experiencing some server problems on our site for the last few days.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic