I am trying to write the equivalent of the following using Java 8 ForEach to encode an array of Strings.
public static void encode(String... stringsToEncode) {
for (int i = 0; i < stringsToEncode.length; i++) {
stringsToEncode[i] = URLEncoder.encode(stringsToEncode[i], "UTF-8");
}
}
// stringsToEncode = 10+111569+++8 as expected.
I have implemented the following:
public static void encodeUsingForEach(String... stringsToEncode)
List<String> listOfStrings = Arrays.asList(stringsToEncode);
listOfStrings.forEach(s -> {
try {
s = URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 is unknown");
}
});
}
// listOfStrings = [10 11, 156, 9 8]
What am I missing so that the output of encodeUsingForEach() is equivalent to that of the encode() method?