• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to maintain order in list

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, for pointing me what i understood wrong. Thanks again.

Regards,
Mick
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic