I wanted to simplify this method so that I didn't need an ArrayList, that is, that only with lambdas I could stream the list of integers, analyze who is even or odd, add their corresponding letter and concatenate in a string
public static String o1e3method(List<Integer> list) {
List<String> n = new ArrayList<>();
list.forEach(x -> {
if (x % 2 == 0) {
n.add("e" + x);
}
else {
n.add("o" + x);
}
});
return String.join(", ", n);
}
In other words, I wanted something that was something like this:
public static String o1e3b(List<Integer> list) {
return list.stream()
.map(Object::toString)
.forEach(x -> {
if (Integer.parseInt(x) % 2 == 0) {
x = "e" + x;
}
else {
x = "o" + x;
}
})
.collect(Collectors.joining(", "));
}
but I can't do this because the forEach is a void method and doesn't return what to collect.