Refactoring Java Loops to Streams Automatically
Alessandro Midolo, Emiliano Tramontana · 2021
Java, starting from version 8, adopted the functional paradigm, comprising lambda expressions, streams, functional interfaces, etc. This paper proposes an approach to automatically refactor for loops to streams. In our approach, code implementing for loops is checked against a set of preconditions and matched to proposed templates associating a for loop to a proper equivalent stream-based code. Then, matching loops can be replaced with the equivalent code, according to the recognised template, embedding selected statements extracted from the original version. The approach has been validated on several open source projects, showing that the proposed templates are effective and transformations preserve the behaviour.