英文:
How to simplify a lambda method that receives a list of integers and returns a string by adding a letter before the number?
问题
我想简化这个方法,这样我就不需要一个 ArrayList
,也就是说,只需要使用 lambda 表达式,我就可以对整数列表进行流操作,分析哪些是偶数或奇数,添加它们对应的字母,并拼接成一个字符串。
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);
}
换句话说,我想要的是类似这样的代码:
public static String o1e3b(List<Integer> list) {
return list.stream()
.map(Object::toString)
.map(x -> {
if (Integer.parseInt(x) % 2 == 0) {
return "e" + x;
}
else {
return "o" + x;
}
})
.collect(Collectors.joining(", "));
}
但是我不能这样做,因为 forEach
是一个返回 void
的方法,不能返回需要收集的值。
英文:
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.
答案1
得分: 4
forEach
是一个终端操作,它不会返回Stream。您需要一个中间操作来转换您的数字并返回转换后的流,然后您将对其进行收集。
请注意,您有一个多余的映射到字符串,您根本不需要它。
我建议:
public static String o1e3b(List<Integer> list) {
return list.stream()
.map(x -> x % 2 == 0 ? "e" + x : "o" + x)
.collect(Collectors.joining(", "));
}
以防万一您不熟悉,可以查看一下什么是Ternary Operator(三元运算符),我在映射函数的主体中使用了它。
英文:
forEach
is a terminal operation, which does not return Stream. You need an intermediate operation to transform your numbers and return transformed stream, which you will later collect.
Note, that you have a redundant mapping to String, which you simply do not need.
I would recommend:
public static String o1e3b(List<Integer> list) {
return list.stream()
.map(x -> x % 2 == 0 ? "e" + x : "o" + x)
.collect(Collectors.joining(", "));
}
Just in case you are not familiar, see what is Ternary Operator, which I use as a body of the mapper function.
答案2
得分: 3
你可以简单地使用map
转换为字符串形式:
return list.stream()
.map(x -> x % 2 == 0 ? "e" + x : "o" + x)
.collect(Collectors.joining(", "));
英文:
You can simply map
to String as:
return list.stream()
.map(x -> x % 2 == 0 ? "e" + x : "o" + x)
.collect(Collectors.joining(", "));
答案3
得分: 1
你的想法是对的,但是不应该使用forEach
,而应该使用map
。此外,请注意没有必要将整数转换为字符串,然后再解析它们:
public static String o1e3b(List<Integer> list) {
return list.stream()
.map(x -> {
if (x % 2 == 0) {
return "e" + x;
} else {
return "o" + x;
}
})
.collect(Collectors.joining(", "));
}
英文:
You have the right idea, but instead of forEach
, you should use map
. Also note there's no reason to convert the integers to string and then parse them again:
public static String o1e3b(List<Integer> list) {
return list.stream()
.map(x -> {
if (x %2 == 0) {
return "e" + x;
} else {
return "o" + x;
}
})
.collect(Collectors.joining(", "));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论