This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to maintain order in list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to maintain order in list" Watch "How to maintain order in list" New topic
Author

How to maintain order in list

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Apr 26, 2011
Posts: 34

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


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Mick Foley
Greenhorn

Joined: Mar 07, 2011
Posts: 4
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


Never give up your goal
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mick Foley
Greenhorn

Joined: Mar 07, 2011
Posts: 4
Thanks Rob, for pointing me what i understood wrong. Thanks again.

Regards,
Mick
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to maintain order in list
 
Similar Threads
Sessions without cookies
Help on one interview question
How to survive in a workplace without technical document
how to find whether the object is singleton or not
Can we override or Overload Main mathod