I have a program that uses ArrayList<T>, and this type of T also implements Comparable<T>. I need to keep this list sorted.
Now, when I insert a new element, I add it to ArrayList, and then call Collections.sort(myArrayList).
Is sorting with Collections.sortevery time I insert a new element seriously hurt the runtime complexity?
Is there a more suitable data structure that I can use to always sort the list? I know the structure called PriorityQueue, but I also need to be able to get list items by index.
EDIT: In my particular case, inserting a new item is much less than getting an existing item, so in the end, good advice could also be left with ArrayList, since it got constant time complexity when getting the item. But if you know anything else ...
source
share