Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?
- by david
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.