How to effectively divide an already sorted list into groups, possibly using Guava?

I have an array that logically contains several groups of elements, where each group is adjacent in the array. (It was sorted using the "order by" database). It would be most pure to go through this as a collection of collections (in the free sense, an iterator of iterators is in order), where a top-level iterator will return one lower-level iterator for each group. This may work similarly to the Guava () section, but using the passed comparator to figure out where to split the input.

There are many inefficient ways to do this, for example, using Guava MultiMap. Is there a simple, ready-made, effective way to do this that uses ordering?

+4
source share
1 answer

Write your own iterators.

The first returns instances of the second.

They have an index!

The second returns true for hasNext () while the index is on elements of the same group. when second's.sasNext () returns a fake call first.hasNext () and then first.next (), etc.

This should be 5-10 lines of written code (assuming your ID environment does all of the class, method and brackets).

This is an effective and not too ugly way to do things. If you want to be more efficient, just go through the array by checking the group condition in a for loop. This is even less code.

+1
source

All Articles