java - Change part of code to functional paradigm -
i have few lines of code, i'm trying convert functional paradigm. code is:
private string test(list<string> strings, string input) { (string s : strings) { input = input.replace(s, ", "); } return input; }
i need make 1 instruction chain. must replace strings given list coma in given string input. tried map method, no success. i'm aware can if appended input string list @ beginning , call map then, list immutable, cannot that.
i believe can simple reduce:
strings.stream().reduce(input, (in, s) -> in.replace(s, ", "));
it takes input, , replaces each occurence of first string ", ". takes result, , uses input along next string, , repeats every item in list.
as louis wasserman points out, approach cannot used parallelstream
, won't work if want parallelization.
Comments
Post a Comment