wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "collection" Watch "collection" New topic
Author

collection

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

what is difference between "Ordered" and "Sorted" ?


My one regret in life is that I am not someone else.
- Woody Allen
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Sorry , i found similar thread.http://www.coderanch.com/t/409250/java/java/Ordering-Sorting
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
And that old thread didn’t help at all, did it?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

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


Yes, it did.As well as i found another, http://www.coderanch.com/t/269109/java-programmer-SCJP/certification/Difference-betwwen-Sorted-Ordered
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
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.
    vinay chaturvedi
    Greenhorn

    Joined: Jan 16, 2012
    Posts: 14
    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

    Joined: Dec 05, 2012
    Posts: 305

    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.
    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 2996
        
        9
    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

    Joined: Jan 16, 2012
    Posts: 14
    Thanks for the corrections Mike!!
    meeta gaur
    Ranch Hand

    Joined: Dec 05, 2012
    Posts: 305

    Thank you mike, i always confuse here to explain difference between them.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: collection