This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Ordering and Sorting of collections 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 "Ordering and Sorting of collections" Watch "Ordering and Sorting of collections" New topic
Author

Ordering and Sorting of collections

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi all

What is the difference between these two terms as far as collections are concerned? I know that sorting is the arrangement of the elements of a collection according to a rule(like alphabetically, ascending/descending). Then what is ordering?

~ Mansukh


~ Mansukh
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

An ordered collection is simply a collection that has an order. So every time you iterate through the collection you'll get the elements in the same order. Often (though not always) the order will simply be the order you added things to the collection.

So in terms of common Java collections:

- HashSet is unordered
- ArrayList, LinkedList, LinkedHashSet are ordered
- TreeSet is sorted

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Matthew Brown wrote:An ordered collection is simply a collection that has an order. So every time you iterate through the collection you'll get the elements in the same order. Often (though not always) the order will simply be the order you added things to the collection.

So in terms of common Java collections:

- HashSet is unordered
- ArrayList, LinkedList, LinkedHashSet are ordered
- TreeSet is sorted



Thanks man..
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
I would add that all sorted collections are also ordered. Otherwise sorting would be impossible and meaningless. But not all ordered collections are sorted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ordering and Sorting of collections
 
Similar Threads
Collection framework doubt , what is the difference between Ordering and Sorting
Suggestion about data structure to be used for piecewise defined functions.
Ordered and Sorted
About Collections (Characterist)
ordering values in Arraylist