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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -