Reputation: 680
I'm looking for a collection that maintains sorted order as well as index access. My program will have hundreds of thousands of iterations, so I don't want to keep calling Collections.sort, as that would be too costly.
Upvotes: 1
Views: 582
Reputation: 159106
None of the standard collections that come with Java supports this, but you can implement your own.
One way to implement such a collection would be to use a sorted array. Index lookups are easy. Value lookups could use Arrays.binarySearch()
. Inserting a new value would also use binarySearch()
to find the insertion point, then shift the remainder of the values to make room for the new value, auto-extending the array as needed.
Upvotes: 2