Need a collection of strings where elements inserted needed to be sorted and also non-duplicate, can be retrieved through index.
- I can use
TreeSetwhich removes duplicates and sorts everything in
order but cannot retrieve through index. for retrieving through
index, i can makeArrayListandaddAllelements to it, but this
addAlltakes lot of time.
or
- I can use an
ArrayList, insert required and then remove duplicates by some other method, then usingCollections.sortmethod to sort elements.
But the thing is, all these take time, is there any straight-way to achieve this, a collection -sorted, non-duplicate, with O(1) random access by index.
There’s a Data Type in the commons collection called SetUniqueList that I believe meetsyour needs perfectly. Check it out:
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/list/SetUniqueList.html