Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

collections- Odered,sorted etc

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  •  
    Bartender
    Posts: 4568
    9
    • Likes 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 275
    jQuery Eclipse IDE Spring
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    author
    Posts: 23928
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Marshal
    Posts: 76474
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic