如何简化接收整数列表并在数字前添加字母后返回字符串的 lambda 方法?

huangapple go评论73阅读模式
英文:

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&lt;Integer&gt; list) {
    List&lt;String&gt; n = new ArrayList&lt;&gt;(); 
    list.forEach(x -&gt; {
        if (x % 2 == 0) {
            n.add(&quot;e&quot; + x);
        }
        else {
            n.add(&quot;o&quot; + x);
        }
    });
    return String.join(&quot;, &quot;, n);
}

In other words, I wanted something that was something like this:

public static String o1e3b(List&lt;Integer&gt; list) {
    return list.stream()
        .map(Object::toString)
        .forEach(x -&gt; {
            if (Integer.parseInt(x) % 2 == 0) {
                x = &quot;e&quot; + x;
            }
            else {
                x = &quot;o&quot; + x;
            }
        })
        .collect(Collectors.joining(&quot;, &quot;));
}

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&lt;Integer&gt; list) {
    return list.stream()
            .map(x -&gt; x % 2 == 0 ? &quot;e&quot; + x : &quot;o&quot; + x)
            .collect(Collectors.joining(&quot;, &quot;));
}

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 -&gt; x % 2 == 0 ? &quot;e&quot; + x : &quot;o&quot; + x)
            .collect(Collectors.joining(&quot;, &quot;));

答案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&lt;Integer&gt; list) {
    return list.stream()
            .map(x -&gt; {
                if (x %2 == 0) {
                    return &quot;e&quot; + x;
                } else {
                    return &quot;o&quot; + x;
                }
            })
            .collect(Collectors.joining(&quot;, &quot;));
}

huangapple
  • 本文由 发表于 2020年9月24日 20:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64047008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定