Suppose I have a list of intervals (sorted by beginning), and I want to split them so that I have a list of overlapping groups of intervals. So for example with Interval
like:
public class Interval { private final int start; private final int end; public Interval(int start,int end){ this.start = start; this.end = end; } public int getStart(){return start;} public int getEnd(){return end;} public String toString(){ return "("+start+","+end+")"; } }
And a List<Interval>
like:
[(0,4),(1,7),(6,10),(13,17),(20,100),(22,31),(60,65)]
I need the output of List<List<Interval>>
:
[[(0,4),(1,7),(6,10)],[(13,17)],[(20,100),(22,31),(60,65)]]
I can code this, but I really enjoy the more functional approach of Java 8 and want to know if there is something like an idiomatic way to do this using Java 8 threads.
I looked at the "group by" styles provided by Collectors , but they do not seem to apply, since I really do not group the classifier - you cannot calculate groups based only on the property of each individual element, you must consider the properties of each element in relation to groups, which have been calculated so far.
Of course, there is an abnormal way to do this in functional languages ββ(although I speak as a person who is not really a functional programmer :-)). How can I do this with threads in Java 8?