Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

collections- Odered,sorted etc  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are ordered, sorted , unordered collections ? Are there any good simple examples that illustrate these concepts quickly without the need to go through unnecessary maths and lengthy explanations.
Please refrain form giving links to sun/oracle documentation, they are ok for reference BUT NOT for beginners.

PS : my confusion is that ordered and sorted is the same, so why use two words ? Why not call both ordered ?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We say a collection is
  • ordered, if the order of iteration is predictable. For example, the insertion order is remembered in LinkedHashSet. So, when you do a get() over that collection, you'll get back the elements in the same order, but in a normal HashSet that wont be possible - the order is not predictable.
  • sorted, if the order in which the elements are obtained as a result of get() is sorted(either naturally or via comparator) Example - TreeSet
  • unsorted, if the elements are not by default sorted in any way. Example - ArrayList, HashSet
  •  
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So let's say you put the following into your collection: {9, 5, 2, 7, 8} - in that order

    - With an ordered collection, when you iterate you'll get {9, 5, 2, 7, 8}

    - With a sorted collection, you'll get {2, 5, 7, 8, 9}

    - With an unordered collection, you might get {8, 7, 5, 2, 9}, you might get {2, 9, 5, 7, 8}. You simply can't be sure.
     
    Aniruddh Joshi
    Ranch Hand
    Posts: 275
    Eclipse IDE jQuery Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Go by the literal meaning of "Ordered" and "Sorted"
     
    Rahul Sudip Bose
    Ranch Hand
    Posts: 637
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matthew Brown wrote:So let's say you..........You simply can't be sure.


    Thank you very much, i feel much better now . This is the kind of answer that i was looking for. Is there any book/website that explains data-structures (and algos) too using simple/daily-life examples instead of delving into complicated maths and theories even when they are not required (a lot of book authors do this) .

    More suggestions/answers are also welcome.

     
    Rahul Sudip Bose
    Ranch Hand
    Posts: 637
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matthew Brown wrote:So let's say you put the following into your collection: {9, 5, 2, 7, 8} - in that order
    - With a sorted collection, you'll get {2, 5, 7, 8, 9}


    How do we define our own criteria for sorting. Can sorted also mean descending order like - 9,8,7,5,2 ? or
    all odds first (in ascending) order and evens later (in descending order) ie 5,7,9,8,2 ?
    And if our example has strings, could it mean alphabetical order/dictionary order ?
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rahul Sudip Bose wrote:Can sorted also mean descending order like - 9,8,7,5,2 etc ?
    And if our example has strings, could it mean alphabetical order/dictionary order ?

    Yes, it could do. With sorted collections you generally have a choice. You can use the "natural ordering" - which is the ordering defined by the implementation of the Comparable interface (if the class implements it). So Strings are sorted alphabetically (look at String.compareTo), and numbers are sorted in increasing order (look at Integer.compareTo for an example).

    But you can also define your own ordering, if you want to, by creating your own class implementing the Comparator interface, and passing that to the collection. And that can apply whatever rule you want. If you want your numbers in descending order, or your Strings sorted by length, you can do that.
     
    Henry Wong
    author
    Sheriff
    Posts: 23283
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rahul Sudip Bose wrote:Please refrain form giving links to sun/oracle documentation, they are ok for reference BUT NOT for beginners.


    While I agree that the JavaDoc does need some work, I disagree with this -- learning how to look up info from a reference is an important skill for everyone, even beginners. Not saying that you should only get JavaDoc references, just saying that you shouldn't avoid the JavaDoc. Just my two cents.

    Henry
     
    Campbell Ritchie
    Marshal
    Posts: 55687
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is probably not a good idea to go on about a Collection being "ordered"; it is not really a good term. But the descriptions above are, I am sure, correct. By the way, what about a Stack? What do you get it you write this?








    Answer: you get 5 4 3 2 1. You are better off using ArrayDeque for a stack implementation.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!