I've a requirement where I would like to use the Java Stream Api to process a stream of events from a system and apply a data cleanup process to remove repeated events. This is removing the same event repeated multiple times in sequence, not creating a list of distinct events. Most of the Java Stream api examples available online target creating a distinct output from a given input.
Example, for input stream
[a, b, c, a, a, a, a, d, d, d, c, c, e, e, e, e, e, e, f, f, f]
the output List or Stream should be
[a, b, c, a, d, c, e, f]
My current implementation (not using Stream api) looks like
public class Test {
public static void main(String[] args) {
String fileName = "src/main/resources/test.log";
try {
List<String> list = Files.readAllLines(Paths.get(fileName));
LinkedList<String> acc = new LinkedList<>();
for (String line: list) {
if (acc.isEmpty())
acc.add(line);
else if (! line.equals(acc.getLast()) )
acc.add(line);
}
System.out.println(list);
System.out.println(acc);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Output,
[a, b, c, a, a, a, a, d, d, d, c, c, e, e, e, e, e, e, f, f, f]
[a, b, c, a, d, c, e, f]
I've tried various example with reduce, groupingBy, etc., without success. I can't seem to find a way to compare a stream with the last element in my accumulator, if there is such a possibilty.