• 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: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is difference between "Ordered" and "Sorted" ?
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , i found similar thread.https://coderanch.com/t/409250/java/java/Ordering-Sorting
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And that old thread didn’t help at all, did it?
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And that old thread didn’t help at all, did it?



Yes, it did.As well as i found another, https://coderanch.com/t/269109/java-programmer-SCJP/certification/Difference-betwwen-Sorted-Ordered
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am surprised you found that thread any help at all. The second one you found is hardly better.
The two words are not strictly defined, and you should not consider two types of collection. You should consider three:-
  • Ordered collections: One can predict which element will be found when by the Iterator by knowing when that element was added to the collection.
  • Sorted collections: One can predict which element will be found when by the Iterator by knowing the relative values of the elements.
  • Unordered collections: One cannot predict which element will be found when by the Iterator from their values nor by knowing which was added when.

  • Lists Stacks and most Queues are ordered, Sets and Maps are usually unordered, but some sets (as you will have seen from the second link you posted) are sorted. You will probably find more in the Java Tutorials.
     
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ordered means the order in which the elements are added in a collection is maintained.

    For Ex : List is an ordered collection of objects.

    Sorted means the elements added in to a collection will be added in their natural sorting order.

    For Ex : SortedSet is a sorted collection of objects.

    See the below code :



    Output :

    List values are : [C, B, A]
    Set values are : [A, C, D]

    **The List values are in the order in which they are added in to it.

    **The Set values are automatically sorted.

     
    meeta gaur
    Ranch Hand
    Posts: 305
    Tomcat Server Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I am surprised you found that thread any help at all. The second one you found is hardly better.
    The two words are not strictly defined, and you should not consider two types of collection. You should consider three:-

  • Ordered collections: One can predict which element will be found when by the Iterator by knowing when that element was added to the collection.
  • Sorted collections: One can predict which element will be found when by the Iterator by knowing the relative values of the elements.
  • Unordered collections: One cannot predict which element will be found when by the Iterator from their values nor by knowing which was added when.

  • Lists Stacks and most Queues are ordered, Sets and Maps are usually unordered, but some sets (as you will have seen from the second link you posted) are sorted. You will probably find more in the Java Tutorials.



    Thank you, it was great.
     
    Master Rancher
    Posts: 4796
    72
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Multiple errors here:

    Campbell Ritchie wrote:Ordered collections: One can predict which element will be found when by the Iterator by knowing when that element was added to the collection.


    vinay chaturvedi wrote:Ordered means the order in which the elements are added in a collection is maintained.


    No - an ordered collection often maintains insertion order, but it doesn't have to. "Ordered" means it has a well-defined order of some sort, that you can rely on. That's true for a TreeSet, or a PriorityQueue, or a LinkedHashMap, as much as it is for any List. It doesn't have to have anything to do with insertion order. All sorted collections are also ordered. Not all ordered collections are sorted.

    Side note, even for a List (using "insertion order"), it's not enough to know when an object was added. You would also need to know where it was added (at what position). And you need to track whether anything was deleted or replaced. But the point is, the position is predictable, based on the cumulative history of all operations performed on the List.

    vinay chaturvedi wrote:Sorted means the elements added in to a collection will be added in their natural sorting order.



    "Natural sort order" is one way to sort a collection, if the elements implement Comparable. Alternately, you can define a custom Comparator, that can ignore the natural order, and implement some other sort order.

    Campbell Ritchie wrote:Unordered collections: One cannot predict which element will be found when by the Iterator from their values nor by knowing which was added when.



    Or at least, one cannot (and should not try to) predict these things based on anything in the API. For most of these collections it is, in fact, entirely possible to predict their exact behavior, if you know the underlying implementation, and if you care to go through the analysis required to do so. But it's generally a bad idea, and not worth anyone's time.
     
    vinay chaturvedi
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the corrections Mike!!
     
    meeta gaur
    Ranch Hand
    Posts: 305
    Tomcat Server Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you mike, i always confuse here to explain difference between them.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic