Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ordering and Sorting of collections

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add that all sorted collections are also ordered. Otherwise sorting would be impossible and meaningless. But not all ordered collections are sorted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic