• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to maintain order in list

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

in my interview they asked me one like how to maintain order in java.util.list and they asked me is there any predefined method?
i said no.

they said we have one method to maintain order in list.can you people help me out.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that they didn't really know what they were talking about, but that they probably meant the method Collections.sort(List).

I would say that a List is always ordered, period. A list always has an order, which behaves in a consistent, predictable manner. Whether it happens to be the same order as what you want it to be in, that's another question.

Perhaps they want a way to make sure it is sorted, as opposed to merely ordered? In that case, use Collections.sort(). Easy.

Note that Collections.sort() doesn't really maintain sort order. It just sorts the list, once. If you need something that maintains sort order continually, consider using a SortedSet, like TreeSet, rather than a List. Note that this is not possible if you need to be able to allow duplicates in the List. If that's a problem, consider Google Guava's TreeMultiset instead.
 
Badal Chowdhary
Ranch Hand
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shyam,

List is already ordered. An ordered collection preserves the order of elements added in it.

You don't have to do anything special to order elements in a list.

Check out the first sentence in API:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/List.html

Thanks,
Badal
 
Mick Foley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wrote a simple program to check if the list always returns an ordered collection items. For me it does not seems working. I given the below program and output i got, can you tell me if i do something wrong here



But the output seems to be same like how the values were added to the list. It gets sorted only after using the Collections.sort(list). Kindly can you clarify if i do wrong here.


Regards,
Mick
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't do anything wrong, I think you understood something wrong. We've been trying to tell you that any list is ordered. The problem is the definition of ordered. You're thinking that ordered means sorted, but in this context ordered means that the list has a specific order. That's not sorted order but insertion order. To make the list have a sorted order you need to manually keep it sorted; either by manually inserting elements at the right place, or by calling Collections.sort after each insertion.
 
Mick Foley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, for pointing me what i understood wrong. Thanks again.

Regards,
Mick
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic