• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Sybex 816 Chapter 3 Assessment Question 16 [tb617624.JaSE11PrgIISG.c03.02] possible erratum

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question statement:

Suppose that you need to work with a collection of elements that need to be sorted in their natural order, and each element has a unique text id that you want to use to store and retrieve the record.
Which of the following collections classes in the java.util package best suits your needs for this scenario?



A. ArrayList
B. HashMap
C. HashSet
D. TreeMap
E. TreeSet
F. None of the above



The test says the correct answer is D. I think the correct answer should be E because none of the data structures on the list satisfy what the problem statement is asking.

Elements that need to be sorted in their natural order -> means
1. The elements must implement Comparable
2. No custom Comparator is allowed when creating TreeMap
3. Since TreeMap only sorts its keys, the elements themselves must be used as keys, and not their uniqueID.

Each element has a unique text id that you want to use to store and retrieve the record -> means
1. This statement most likely means that we now want to conveniently retrieve the element by specifying its uniqueID on retrieval. But then this contradicts with the statement above, which requires that the keys being the element themselves.
2. The only way to retrieve the element we want by specifying the uniqueID now would be to loop through all the entries, which is not efficient at all, and other answers such as ArrayList or HashSet can do the same thing.

To achieve what the question is asking, we need a data structure that sorts its values by natural order(as defined by Comparable) and the keys being unique Ids.

Code I used to test:

 
Marshal
Posts: 26381
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D or E? So your decision is the same as "Map or Set?" And since the requirement is "each element has a unique text id that you want to use to store and retrieve the record" that means you want a Map (where key -> value) and not a Set (where you only have values). So D is correct.

(Your posted example, very competent I have to say, does use a TreeMap.)
 
Olivia Johnson
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:D or E? So your decision is the same as "Map or Set?" And since the requirement is "each element has a unique text id that you want to use to store and retrieve the record" that means you want a Map (where key -> value) and not a Set (where you only have values). So D is correct.

(Your posted example, very competent I have to say, does use a TreeMap.)



But how do you satisfy the requirement that Elements that need to be sorted in their natural order? If we use the TreeMap and the unique ID(String) as the keys, hence TreeMap<String, Element>, then only the keys (unique IDs) are sorted, but not the Elements themselves.
 
Paul Clapham
Marshal
Posts: 26381
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That concerned me too, but clearly TreeSet still isn't the way to go. So I checked the documentation for TreeMap and it says:

The docs wrote:The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time



As you say, using the natural ordering of the keys isn't what you want. You want the natural ordering of the elements themselves, which to me says you have to provide a Comparator when building the TreeMap.
 
author & internet detective
Posts: 40487
823
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel like you are reading into this. I was thinking that natural order was the sort order of the unique text id. It's part of the element and a logical choice for Comparable.
 
Olivia Johnson
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:I feel like you are reading into this. I was thinking that natural order was the sort order of the unique text id. It's part of the element and a logical choice for Comparable.



Hi Jeanne, thank you for replying.

If the elements' Comparable uses Unique Text Id, then that is fair.

The question did not mention what natural order is implemented for the Elements, leading me to assume that Comparable might have been implemented in some other ways.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic