There are lots of questions on Stack Overflow about how to remove items from a collection while iterating over it (specifically in Java). This question has a decent set of options of how to do it. However, none of the conditions for removal described in those answers are functions of the entry's relationship to its neighboring entries. As an example...
Imagine I have a list of x,y points, and I want to reduce the list such that no series of 3 or more points are colinear. Put another way, I want to be able to reduce the list of points such that they describe the start/end points of a series of line segments such that no two segments are are subsections of a single larger segment. For instance, a given set might look like
[ {0,0}, {3,0}, {5,2}, {7,4}, {10,4} ], and I would want to reduce it to the set
[ {0,0}, {3,0}, {7,4}, {10,4} ], because the the {5,2} point is colinear with both {3,0} and {7,4}. The test of colinearity is dependent on the previous and subsequent point in the set, so it's impossible to know if a given point needs to be removed based on that point alone, which is a requirement for most of the options in the answer linked above, like the removeIf()
method.
My question is simple: does there exist in Java a way of reducing a list in this way in one pass of the dataset? Obviously I could iterate over it once, test each point for colinearity with its neighbours, and store the indices of points to remove, and then in a second pass take only the wanted points and build up a new set.