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.
source share