First time question asker here so please go easy on me. :) Anyway, I am not sure if this is possible using a Java 8 stream but I am very interested in learning.
Let's say I have the following ordered list of numbers:
List<Integer> myList = Arrays.asList(1, 2, 3, 7, 9, 12, 13, 15);
Now, I want to split this list into multiple lists when the difference between elements is greater than 2. Therefore, the end result would be three different lists:
{1, 2, 3}
{7, 9}
{12, 13, 15}
I could easily do this exercise using a for loop and comparing the current element to the previous while looping. However, I am wondering if there is a concise way to accomplish this using a Java 8 stream? Like I said before, this is only for my own learning and understanding of Java 8 so if it isn't possible then that's okay.
Thanks in advance for any comments or answers.