• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collection

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My qustion is,

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

A. java.util.Map
B. java.util.Set
C. java.util.List
D. java.util.SortedSet
E. java.util.SortedMap
F. java.util.Collection

Here as we dont want duplicates, only options available are A,B,D,E.
& as the next condition they says natural order should be maintained so can anybody plzz explain what is the natural order i.e the insertion order or sorted order ? & here what will be the answer ?

Thanks in advance
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that it has something to do with the Comparable interface.
So this should be the sorted order (not inserted order)
SortedMap and SortedSet would be fine.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Natural order is the order inwhich they've been entered. So the answer would be SortedSet.
[ January 23, 2006: Message edited by: Vijay Kiran ]
 
Swati Thorve
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

I didnt get u r point, natural order is the insertion order, so how the answer be SortedSet where elements r in sorted order,I think in this case the answer will be only set.Please explain u r logic.

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

the answer is D. Natural order is natural order. :-) That means, it is alphabetical order for Strings, numerical order for Numbers, date-related order for Dates, etc. For your own classes, the natural order is defined by the compareTo method of the Comparable interface which you have to implement in order to be able to add more than one element to the SortedSet (or you have to specify Comparator at the time of SortedSet creation).

The SortedMap is for storing pairs of keys and values, not "elements" as required in your questions. (It is sorted by keys.)

You can use your own Comparator to override the natural order. See the documentation of TreeSet.

Hope that helped.

Jan
 
Vijay Kiran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want to use SortedSet, the elements must implement the Comparable interface. When you are adding elements, the add function casts the elements into Comparable type. Then it would throw the Classcast exception. So in a SortedSet the elements are iterated in the "natural way" of ordering using a comparator.

Sorry, if my previous reply wasn't clear.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic