{"status":"ok","message-type":"work","message-version":"1.0.0","message":{"indexed":{"date-parts":[[2026,4,9]],"date-time":"2026-04-09T14:42:29Z","timestamp":1775745749459,"version":"3.50.1"},"reference-count":49,"publisher":"MDPI AG","issue":"9","license":[{"start":{"date-parts":[[2025,9,12]],"date-time":"2025-09-12T00:00:00Z","timestamp":1757635200000},"content-version":"vor","delay-in-days":0,"URL":"https:\/\/creativecommons.org\/licenses\/by\/4.0\/"}],"content-domain":{"domain":[],"crossmark-restriction":false},"short-container-title":["Future Internet"],"abstract":"<jats:p>Java 8 brought functional programming to the Java language and library, enabling more expressive and concise code to replace loops by using streams. Despite such advantages, for-loops remain prevalent in current codebases as the transition to the functional paradigm requires a significant shift in the developer mindset. Traditional approaches for assisting refactoring loops into streams check a set of strict preconditions to ensure correct transformation, hence limiting their applicability. Conversely, generative artificial intelligence (AI), particularly ChatGPT, is a promising tool for automating software engineering tasks, including refactoring. While prior studies examined ChatGPT\u2019s assistance in various development contexts, none have specifically investigated its ability to refactor for-loops into streams. This paper addresses such a gap by evaluating ChatGPT\u2019s effectiveness in transforming loops into streams. We analyzed 2132 loops extracted from four open-source GitHub repositories and classified them according to traditional refactoring templates and preconditions. We then tasked ChatGPT with the refactoring of such loops and evaluated the correctness and quality of the generated code. Our findings revealed that ChatGPT could successfully refactor many more loops than traditional approaches, although it struggled with complex control flows and implicit dependencies. This study provides new insights into the strengths and limitations of ChatGPT in loop-to-stream refactoring and outlines potential improvements for future AI-driven refactoring tools.<\/jats:p>","DOI":"10.3390\/fi17090418","type":"journal-article","created":{"date-parts":[[2025,9,12]],"date-time":"2025-09-12T13:43:02Z","timestamp":1757684582000},"page":"418","update-policy":"https:\/\/doi.org\/10.3390\/mdpi_crossmark_policy","source":"Crossref","is-referenced-by-count":4,"title":["Refactoring Loops in the Era of LLMs: A Comprehensive Study"],"prefix":"10.3390","volume":"17","author":[{"ORCID":"https:\/\/orcid.org\/0000-0002-9575-8054","authenticated-orcid":false,"given":"Alessandro","family":"Midolo","sequence":"first","affiliation":[{"name":"Dipartimento di Matematica e Informatica, University of Catania, 95125 Catania, Italy"}]},{"ORCID":"https:\/\/orcid.org\/0000-0002-7169-659X","authenticated-orcid":false,"given":"Emiliano","family":"Tramontana","sequence":"additional","affiliation":[{"name":"Dipartimento di Matematica e Informatica, University of Catania, 95125 Catania, Italy"}]}],"member":"1968","published-online":{"date-parts":[[2025,9,12]]},"reference":[{"key":"ref_1","unstructured":"Urma, R.G., Fusco, M., and Mycroft, A. (2018). Modern Java in Action: Lambdas, Streams, Functional and Reactive Programming, Manning Publications Co.. [2nd ed.]."},{"key":"ref_2","doi-asserted-by":"crossref","first-page":"1","DOI":"10.1145\/3133909","article-title":"Understanding the use of lambda expressions in Java","volume":"1","author":"Mazinanian","year":"2017","journal-title":"Proc. ACM Program. Lang."},{"key":"ref_3","first-page":"1","article-title":"Eliminating abstraction overhead of Java stream pipelines using ahead-of-time program optimization","volume":"4","author":"Veileborg","year":"2020","journal-title":"Proc. ACM Program. Lang."},{"key":"ref_4","doi-asserted-by":"crossref","unstructured":"Rosales, E., Ros\u00e0, A., Basso, M., Villaz\u00f3n, A., Orellana, A., Zenteno, A., Rivero, J., and Binder, W. (2022, January 26\u201330). Characterizing Java Streams in the Wild. Proceedings of the 2022 26th International Conference on Engineering of Complex Computer Systems (ICECCS), Hiroshima, Japan.","DOI":"10.1109\/ICECCS54210.2022.00025"},{"key":"ref_5","doi-asserted-by":"crossref","unstructured":"Wehrheim, H., and Cabot, J. (2020). An Empirical Study on the Use and Misuse of Java 8 Streams. Fundamental Approaches to Software Engineering, Springer.","DOI":"10.1007\/978-3-030-45234-6"},{"key":"ref_6","doi-asserted-by":"crossref","unstructured":"Nostas, J., Alcocer, J.P.S., Costa, D.E., and Bergel, A. (2021, January 13\u201316). How Do Developers Use the Java Stream API?. Proceedings of the Computational Science and Its Applications\u2014ICCSA 2021, Cagliari, Italy.","DOI":"10.1007\/978-3-030-87007-2_23"},{"key":"ref_7","doi-asserted-by":"crossref","unstructured":"Champa, A.I., Rabbi, M.F., Nachuma, C., and Zibran, M.F. (2024, January 15\u201316). ChatGPT in Action: Analyzing Its Use in Software Development. Proceedings of the MSR \u201924: 21st International Conference on Mining Software Repositories, Lisbon, Portugal.","DOI":"10.1145\/3643991.3645077"},{"key":"ref_8","doi-asserted-by":"crossref","first-page":"123602","DOI":"10.1016\/j.eswa.2024.123602","article-title":"Exploring ChatGPT\u2019s code refactoring capabilities: An empirical study","volume":"249","author":"DePalma","year":"2024","journal-title":"Expert Syst. Appl."},{"key":"ref_9","doi-asserted-by":"crossref","unstructured":"Bulla, L., Midolo, A., Mongiov\u00ec, M., and Tramontana, E. (2024). EX-CODE: A Robust and Explainable Model to Detect AI-Generated Code. Information, 15.","DOI":"10.3390\/info15120819"},{"key":"ref_10","unstructured":"Fowler, M. (2018). Refactoring: Improving the Design of Existing Code, Addison-Wesley Professional."},{"key":"ref_11","doi-asserted-by":"crossref","first-page":"5","DOI":"10.1109\/TSE.2011.41","article-title":"How We Refactor, and How We Know It","volume":"38","author":"Parnin","year":"2012","journal-title":"IEEE Trans. Softw. Eng."},{"key":"ref_12","unstructured":"Midolo, A., and Tramontana, E. (2021, January 22\u201324). Refactoring Java Loops to Streams Automatically. Proceedings of the CSSE \u201821: International Conference on Computer Science and Software Engineering, Singapore."},{"key":"ref_13","doi-asserted-by":"crossref","unstructured":"Zhang, Z., Xing, Z., Xia, X., Xu, X., and Zhu, L. (2022, January 14\u201318). Making Python code idiomatic by automatic refactoring non-idiomatic Python code with pythonic idioms. Proceedings of the ESEC\/FSE 2022: 30th ACM Joint European Software Engineering Conference and Symposium on the Foundations of Software Engineering, Singapore.","DOI":"10.1145\/3540250.3549143"},{"key":"ref_14","doi-asserted-by":"crossref","first-page":"2827","DOI":"10.1109\/TSE.2024.3420886","article-title":"Automated Refactoring of Non-Idiomatic Python Code with Pythonic Idioms","volume":"50","author":"Zhang","year":"2024","journal-title":"IEEE Trans. Softw. Eng."},{"key":"ref_15","doi-asserted-by":"crossref","unstructured":"Franklin, L., Gyori, A., Lahoda, J., and Dig, D. (2013, January 18\u201326). LambdaFicator: From imperative to functional programming through automated refactoring. Proceedings of the 2013 35th International Conference on Software Engineering (ICSE), San Francisco, CA, USA.","DOI":"10.1109\/ICSE.2013.6606699"},{"key":"ref_16","first-page":"314","article-title":"Deep Learning-Based Code Refactoring: A Review of Current Knowledge","volume":"64","author":"Purnima","year":"2024","journal-title":"J. Comput. Inf. Syst."},{"key":"ref_17","doi-asserted-by":"crossref","first-page":"1819","DOI":"10.1145\/3660788","article-title":"Beyond Code Generation: An Observational Study of ChatGPT Usage in Software Engineering Practice","volume":"1","author":"Khojah","year":"2024","journal-title":"Proc. ACM Softw. Eng."},{"key":"ref_18","doi-asserted-by":"crossref","unstructured":"White, J., Hays, S., Fu, Q., Spencer-Smith, J., and Schmidt, D.C. (2024). ChatGPT Prompt Patterns for Improving Code Quality, Refactoring, Requirements Elicitation, and Software Design. Generative AI for Effective Software Development, Springer.","DOI":"10.1007\/978-3-031-55642-5_4"},{"key":"ref_19","doi-asserted-by":"crossref","unstructured":"Chavan, O.S., Hinge, D.D., Deo, S.S., Wang, Y.O., and Mkaouer, M.W. (2024, January 15\u201316). Analyzing Developer-ChatGPT Conversations for Software Refactoring: An Exploratory Study. Proceedings of the MSR \u201924: 21st International Conference on Mining Software Repositories, Lisbon, Portugal.","DOI":"10.1145\/3643991.3645082"},{"key":"ref_20","doi-asserted-by":"crossref","unstructured":"Guo, Q., Cao, J., Xie, X., Liu, S., Li, X., Chen, B., and Peng, X. (2024, January 14\u201320). Exploring the Potential of ChatGPT in Automated Code Refinement: An Empirical Study. Proceedings of the ICSE \u201924: 46th IEEE\/ACM International Conference on Software Engineering, Lisbon, Portugal.","DOI":"10.1145\/3597503.3623306"},{"key":"ref_21","doi-asserted-by":"crossref","unstructured":"Siddiq, M.L., Roney, L., Zhang, J., and Santos, J.C.D.S. (2024, January 15\u201316). Quality Assessment of ChatGPT Generated Code and their Use by Developers. Proceedings of the MSR \u201824: 21st International Conference on Mining Software Repositories, Lisbon, Portugal.","DOI":"10.1145\/3643991.3645071"},{"key":"ref_22","doi-asserted-by":"crossref","unstructured":"Tufano, R., Mastropaolo, A., Pepe, F., Dabic, O., Di Penta, M., and Bavota, G. (2024, January 15\u201316). Unveiling ChatGPT\u2019s Usage in Open Source Projects: A Mining-based Study. Proceedings of the MSR \u201924: 21st International Conference on Mining Software Repositories, Lisbon, Portugal.","DOI":"10.1145\/3643991.3644918"},{"key":"ref_23","doi-asserted-by":"crossref","first-page":"1340","DOI":"10.1109\/TSE.2024.3382365","article-title":"ChatGPT vs SBST: A Comparative Assessment of Unit Test Suite Generation","volume":"50","author":"Tang","year":"2024","journal-title":"IEEE Trans. Softw. Eng."},{"key":"ref_24","doi-asserted-by":"crossref","unstructured":"Gyori, A., Franklin, L., Dig, D., and Lahoda, J. (2013, January 18\u201326). Crossing the gap from imperative to functional programming through refactoring. Proceedings of the ESEC\/FSE 2013: 9th Joint Meeting on Foundations of Software Engineering, Saint Petersburg, Russia.","DOI":"10.1145\/2491411.2491461"},{"key":"ref_25","unstructured":"Midolo, A., and Tramontana, E. (2025, August 28). Replication Package. Available online: https:\/\/github.com\/AleMidolo\/Refactoring-Loops-in-the-Era-of-LLMs-A-Comprehensive-Study."},{"key":"ref_26","doi-asserted-by":"crossref","unstructured":"Kapus, T., Ish-Shalom, O., Itzhaky, S., Rinetzky, N., and Cadar, C. (2019, January 22\u201326). Computing summaries of string loops in C for better testing and refactoring. Proceedings of the PLDI 2019: 40th ACM SIGPLAN Conference on Programming Language Design and Implementation, Phoenix, AZ, USA.","DOI":"10.1145\/3314221.3314610"},{"key":"ref_27","doi-asserted-by":"crossref","first-page":"102476","DOI":"10.1016\/j.scico.2020.102476","article-title":"Safe automated refactoring for intelligent parallelization of Java 8 streams","volume":"195","author":"Khatchadourian","year":"2020","journal-title":"Sci. Comput. Programm."},{"key":"ref_28","doi-asserted-by":"crossref","unstructured":"Midolo, A., and Tramontana, E. (2022, January 29\u201331). An API for Analysing and Classifying Data Dependence in View of Parallelism. Proceedings of the ICCCM \u201922: 10th International Conference on Computer and Communications Management, Okayama, Japan.","DOI":"10.1145\/3556223.3556232"},{"key":"ref_29","doi-asserted-by":"crossref","unstructured":"Stein, B., Clapp, L., Sridharan, M., and Chang, B.Y.E. (2018, January 3\u20137). Safe stream-based programming with refinement types. Proceedings of the ASE \u201918: 33rd ACM\/IEEE International Conference on Automated Software Engineering, Montpellier, France.","DOI":"10.1145\/3238147.3238174"},{"key":"ref_30","unstructured":"The PEP Editors (2025, August 28). Python Enhancement Proposals. Available online: https:\/\/peps.python.org."},{"key":"ref_31","doi-asserted-by":"crossref","unstructured":"Midolo, A., and Penta, M.D. (2025, January 27\u201328). Automated Refactoring of Non-Idiomatic Python Code: A Differentiated Replication with LLMs. Proceedings of the 2025 IEEE\/ACM 33rd International Conference on Program Comprehension (ICPC), Ottawa, ON, Canada.","DOI":"10.1109\/ICPC66645.2025.00020"},{"key":"ref_32","doi-asserted-by":"crossref","first-page":"26","DOI":"10.1007\/s10515-025-00500-0","article-title":"Exploring the potential of general purpose LLMs in automated software refactoring: An empirical study","volume":"32","author":"Liu","year":"2025","journal-title":"Autom. Softw. Eng."},{"key":"ref_33","unstructured":"Cordeiro, J., Noei, S., and Zou, Y. (2024). An Empirical Study on the Code Refactoring Capability of Large Language Models. arXiv."},{"key":"ref_34","doi-asserted-by":"crossref","unstructured":"Pomian, D., Bellur, A., Dilhara, M., Kurbatova, Z., Bogomolov, E., Bryksin, T., and Dig, D. (2024, January 6\u201311). Next-Generation Refactoring: Combining LLM Insights and IDE Capabilities for Extract Method. Proceedings of the 2024 IEEE International Conference on Software Maintenance and Evolution (ICSME), Flagstaff, AZ, USA.","DOI":"10.1109\/ICSME58944.2024.00034"},{"key":"ref_35","doi-asserted-by":"crossref","unstructured":"AlOmar, E.A., Venkatakrishnan, A., Mkaouer, M.W., Newman, C., and Ouni, A. (2024, January 15\u201316). How to refactor this code? An exploratory study on developer-ChatGPT refactoring conversations. Proceedings of the MSR \u201924: 21st International Conference on Mining Software Repositories, Lisbon Portugal.","DOI":"10.1145\/3643991.3645081"},{"key":"ref_36","doi-asserted-by":"crossref","unstructured":"Fraser, G., and Arcuri, A. (2011, January 5\u20139). EvoSuite: Automatic test suite generation for object-oriented software. Proceedings of the ESEC\/FSE \u201911: 19th ACM SIGSOFT Symposium and European Conference on Foundations of Software Engineering, Szeged, Hungary.","DOI":"10.1145\/2025113.2025179"},{"key":"ref_37","doi-asserted-by":"crossref","unstructured":"Dabic, O., Aghajani, E., and Bavota, G. (2021, January 17\u201319). Sampling Projects in GitHub for MSR Studies. Proceedings of the 2021 IEEE\/ACM 18th International Conference on Mining Software Repositories (MSR), Madrid, Spain.","DOI":"10.1109\/MSR52588.2021.00074"},{"key":"ref_38","unstructured":"maven (2024, September 02). Mavenrepository. Available online: https:\/\/mvnrepository.com\/popular."},{"key":"ref_39","unstructured":"Smith, N., Van Bruggen, D., and Tomassetti, F. (2017). Javaparser: Visited, Leanpub."},{"key":"ref_40","doi-asserted-by":"crossref","first-page":"1","DOI":"10.1145\/3715908","article-title":"Structured Chain-of-Thought Prompting for Code Generation","volume":"34","author":"Li","year":"2025","journal-title":"ACM Trans. Softw. Eng. Methodol."},{"key":"ref_41","first-page":"1","article-title":"AceCoder: An Effective Prompting Technique Specialized in Code Generation","volume":"33","author":"Li","year":"2024","journal-title":"ACM Trans. Softw. Eng. Methodol."},{"key":"ref_42","unstructured":"Liu, C., Bao, X., Zhang, H., Zhang, N., Hu, H., Zhang, X., and Yan, M. (2023). Improving ChatGPT Prompt for Code Generation. arXiv."},{"key":"ref_43","unstructured":"Raven, O. (2024, September 03). Magpie. Available online: https:\/\/github.com\/openraven\/magpie."},{"key":"ref_44","unstructured":"Vertispan (2024, September 03). j2clmavenplugin. Available online: https:\/\/github.com\/Vertispan\/j2clmavenplugin."},{"key":"ref_45","unstructured":"IridiumIdentity (2024, September 03). Iridium. Available online: https:\/\/github.com\/IridiumIdentity\/iridium."},{"key":"ref_46","unstructured":"OpenRefine (2024, September 03). Openrefine. Available online: https:\/\/github.com\/OpenRefine\/OpenRefine."},{"key":"ref_47","unstructured":"Rapiddweller (2024, September 03). Rapiddweller-Benerator-ce. Available online: https:\/\/github.com\/rapiddweller\/rapiddweller-benerator-ce."},{"key":"ref_48","unstructured":"Liu, F., Liu, Y., Shi, L., Huang, H., Wang, R., Yang, Z., and Zhang, L. (2024). Exploring and evaluating hallucinations in llm-powered code generation. arXiv."},{"key":"ref_49","first-page":"e35179","article-title":"Artificial hallucinations in ChatGPT: Implications in scientific writing","volume":"15","author":"Alkaissi","year":"2023","journal-title":"Cureus"}],"container-title":["Future Internet"],"original-title":[],"language":"en","link":[{"URL":"https:\/\/www.mdpi.com\/1999-5903\/17\/9\/418\/pdf","content-type":"unspecified","content-version":"vor","intended-application":"similarity-checking"}],"deposited":{"date-parts":[[2025,10,9]],"date-time":"2025-10-09T18:44:43Z","timestamp":1760035483000},"score":1,"resource":{"primary":{"URL":"https:\/\/www.mdpi.com\/1999-5903\/17\/9\/418"}},"subtitle":[],"short-title":[],"issued":{"date-parts":[[2025,9,12]]},"references-count":49,"journal-issue":{"issue":"9","published-online":{"date-parts":[[2025,9]]}},"alternative-id":["fi17090418"],"URL":"https:\/\/doi.org\/10.3390\/fi17090418","relation":{},"ISSN":["1999-5903"],"issn-type":[{"value":"1999-5903","type":"electronic"}],"subject":[],"published":{"date-parts":[[2025,9,12]]}}}