• Post Reply Bookmark Topic Watch Topic
  • New Topic

Map or Set question  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got this test question which I wasn't sure of the answer. Pls help.
Question: Your programming problem is to create a list of unique values of part ID numbers in a large collection of data representing orders. Furthermore, it would be nice if the list was in sorted order. You have decided to use one of the collection classes in the java.util package to construct this list. Which of the following interfaces should the ideal class implement?

A. Map

B. SortedMap

C. Set

D. SortedSet
My answer is either B or D. I just cannot make up my mind. Any insight to your answer would be much appreciated.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my oppinion you should store a list. So Solutione D is correct.
The Map always stores key-value pairs. The sorted map simply guarantees that the keys are in stored in ascending order.
The SortedSet stores a list of elements in sorted order. So difference is:
SortedMap : Keys are sorted (elements sorting depends on the keys)
SortedSet : Elements are sorted (no keys exist)
Olli
[ July 31, 2003: Message edited by: Oliver Refle ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D.
The question states that the list in sorted order and hints that you should be able to retrieve orders by their Id.
You should use a SortedMap (such as TreeMap) mapping OrderId -> Order.
D.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I see nothing about needing to retrieve orders. It's a reasonable possibility in the real world, but it was not asked for. They just ask for the part numbers. More specifically, unique part numbers, in [i]sorted[/] order.
Oliver - you threw me by saying "list"; at first I thought you meant a List, which seemed to come from left field. But on rereading I see you meant "list" in a more informal sense.
 
Stephen Lim
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh, thanks guys.
Oh by the way, where do you think this kinda questions are adapted from?
(Besides, SCJP). Do you reckon Sun has got another set of exams that has this sort of questions?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!