Highest and lowest elements in trove TIntSet?

285 views Asked by At

TIntSet is "sorted set" by sense, i.e. it's elements have natural order.

Unfortunately, I can't find any methods similar to first() and last().

Is it possible to overcome this lack somehow?

4

There are 4 answers

0
leventov On BEST ANSWER

fastutil is all ways better than Trove. There is IntSortedSet interface with firstInt() and lastInt() methods.

0
pkozlov On

Why do you think this set is sorted? It looks like it's not. It doesn't implement SortedSet or NavigableSet interfaces from JDK collections framework - there are no such methods there.

first() and last() methods are actually inherited from interface SortedSet https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#first%28%29

0
Ashkrit Sharma On

If you want first/last/cell/floor etc type of option then Tree based set is required and trove does not have anything like that. If you want to extend TIntSet to have such thing then

  • one of the simple option can be maintaining sorted values in parallel int array and use that to serve first/last type of request, but this will required extra memory.

  • another option is that you can use just one array for values but keep is sorted and use binary search to support map API. This can be little slow for get/put as compared to trove but you can save memory because trove has .5 loadfactor

So you can make decision based on trade off you want .

3
Rob Eden On

TIntSet is not sorted (it's a hash set) so in order to find the min or max you would need to iterate all values.