Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Sorted iterator for an unsorted list

Reply
Thread Tools

Sorted iterator for an unsorted list

 
 
Gyruss
Guest
Posts: n/a
 
      04-11-2005
Dear all,

What's the preferred way to get an iterator that iterates over all elements
of an unsorted list, in sorted order? My approach was to create a copy of
the list, sort it and then return the sorted list's iterator.

This seems crude ... is it?

Cheers,
David

public Iterator getSortedIterator() {
List l = new ArrayList();
l.addAll(myCollection.values());
Collections.sort(l, new FooModelComparator());
return l.iterator();
}

private static class FooModelComparator implements Comparator {
public int compare(Object o1, Object o2) {
Foo s1 = (Foo) o1;
Foo s2 = (Foo) o2;
return s1.getId().compareTo(s2.getId());
}
}


 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      04-11-2005
Gyruss wrote:

> Dear all,
>
> What's the preferred way to get an iterator that iterates over all elements
> of an unsorted list, in sorted order?


You mean that you want an iterator that returns the elements in a
sequence different from the List's current one. It is a key
characteristic of a List that it has an inherent order, so there is no
such thing as an unsorted List, just one whose elements are not in the
sequence implied by some chosen sorting scheme.

> My approach was to create a copy of
> the list, sort it and then return the sorted list's iterator.
>
> This seems crude ... is it?


If you need retain the List's current sequence and yet iterate over the
elements in a different sequence, then something like you describe is
nice and simple, and is likely to perform about as well as you can hope.
It's biggest drawback is that the iterator will not provide fail-fast
behavior relative to the original List.

If you do not need to retain the original List's sequence, then it is
better to just sort that List (or keep it in sorted order in the first
place) and use its iterator directly. You will get fail-fast behavior
for free this way (whether you want it or not).

If you cannot sort the original list, but do want fail-fast behavior
then it's going to start getting ugly. You could probably build an
Iterator that leverages Collections.min() and List.subList() to do its
thing, but it's going to be slower and consume more memory than any of
the other options I've discussed.

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorted DataView, but unsorted datalist when bound to the dataview CodeMonkey ASP .Net 1 02-04-2011 10:55 AM
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Unsorted List =?Utf-8?B?Sm9obiBCYW5raGVhZA==?= ASP .Net 4 03-08-2005 12:31 AM



Advertisments