Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

Posted by david on Stack Overflow See other posts from Stack Overflow or by david
Published on 2011-02-04T22:36:29Z Indexed on 2011/02/04 23:26 UTC
Read the original article Hit count: 206

Filed under:
|
|
|
|

In Java you can build up an ArrayList with items and then call:

Collections.sort(list, comparator);

Is there anyway to pass in the Comparator at the time of List creation like you can do with TreeMap? The goal is to be able add an element to the list and instead of having it automatically appended to the end of the list, the list would keep itself sorted based on the Comparator and insert the new element at the index determined by the Comparator. So basically the list might have to re-sort upon every new element added.

Is there anyway to achieve this in this way with the Comparator or by some other similar means?

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about list