2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Sorted collection in Java

Please suggest which collection(s) can/should be used for maintaining a sorted list in Java. I have tried Map and Set, but they weren"t what I was looking for.
userimage
If you want to maintain a sorted list which you will frequently modify (i.e. a structure which, in addition to being sorted, allows duplicates and whose elements can be efficiently referenced by index), then use an ArrayList but when you need to insert an element, always useCollections.binarySearch() to determine the index at which you add a given element. The latter method tells you the index you need to insert at to keep your list in sorted order.
Answer is