4

Possible Duplicate:
sorted collection in java

I was wondering if Java has its own version of Sorted List, or if I need to create my own. I want the list to automatically update itself if something is removed. For example, if I remove something from the start of the list, or even in the middle, I want everything behind it to move up in the list, and the remaining null value space to be removed.

Community
  • 1
  • 1
Hani Honey
  • 2,101
  • 11
  • 48
  • 76

3 Answers3

4

If you're really after the equivalent of a .NET SortedList, which is actually a map ordered by its keys, then the closest equivalent is probably TreeMap. That's actually more like SortedDictionary than SortedList, given that it's a tree rather than just a list, but it's probably the closest available option.

However, what you've described is more like ArrayList, which is similar to .NET's List<T>.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

java.util.PriorityQueue

An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

This is basically a heap that allows reading from the front in order, and allows removal from the middle via Iterator.remove but the iterator does not iterate in any particular order.

If you want something that you can iterate over in order, and don't need dupes, then a TreeSet is your best bet. If you need dupes, then look at a library like Apache common's TreeBag.

Hilikus
  • 9,954
  • 14
  • 65
  • 118
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
3

Well, Java has quite a few list implementations that are smarter than arrays, though it doens't sound like you really want a sorted list from your description.

An ArrayList or LinkedList will do what you want as far as inserting or removing elements:

public Object remove(int index) - Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Do you really want a sorted list, or just something higher-level than an array?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • I'm already using an ArrayList, so I guess it'll be fine. Basically I don't want any null references. I have a loop that is constantly running through this ArrayList, so if something is removed, I don't want there to be a null reference. From what you say, it looks like ArrayList will be fine. – Hani Honey Apr 09 '11 at 18:56
  • Double-check that the performance of ArrayList.remove() is acceptable for large arrays. I've been bitten by the performance of removing a character from the beginning of a StringBuffer. – Thorbjørn Ravn Andersen Apr 09 '11 at 19:01