I have an Integer-collection with filtered row-IDs in which I am trying to search for sequences/ranges to optimise a MySQL select-query. To give you an example:
The Integer-Collection can be either very fragmented:
[1,2,88,101,200] = Sequence(1-2,88,101,200)
Or very entirely:
[1,2,3,4,..,198,199,200] = Sequence(1-200)
Is there any Java-algorithm to find a sequence in the collection or to improve my reading-pattern in general?
How long are your collections? Unless you have millions of items, it is probably fastest to load a collection entirely into memory, sort it and then scan for ranges.
In a sorted list, finding ranges is trivial. Just scan it sequentially; if the next element is not previous element + 1, one range has just ended, and another began.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With