aspose file tools*
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 Spring in Action this week in the Spring 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: 4422
    
    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: 3018
    
  10
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