我不理解使用流来反转字符串的代码。

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

I do not understand the code that reverses a string using streams

问题

请对下面使用流(streams)反转字符串的代码进行解释:

public static String reverse(String test) { 
    return IntStream.range(0, test.length()) 
                    .map(i -> test.charAt(test.length() - i - 1)) 
                    .collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append) 
                    .toString(); 
}
英文:

Please give an explanation for the below code reversing the String with streams:

public static String reverse(String test) { 
    return IntStream.range(0, test.length()) 
                    .map(i -> test.charAt(test.length() - i - 1)) 
                    .collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append) 
                    .toString(); 
}

答案1

得分: 2

这个方法可以分解为5个关键部分:

public static String reverse(String test) {
    return IntStream.range(0, test.length())
            .map(i -> test.charAt(test.length() - i - 1))
            .collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append)
            .toString();
}

在第1行,将字符串传递给方法以便以相反的顺序返回。例如,字符串test可以等于"world"。

在第2行,一系列数字被传递到流中,这些数字与传入的字符串一样长。如果test等于"world",则数字流将为0、1、2、3、4。

在第3行,这些数字被映射到它们在字符串末尾的位置。因此,数字0被映射到字符test.length(5)-i(0)-1。因此,数字0被映射到位置4的字符。这等于"d"。对于所有其他数字也是这样做的,所以0=d,1=l,2=r,3=o,4=w。

在第4行,创建一个新的StringBuilder,并将每个字符附加到StringBuilder中,因此它会按照它们的顺序添加字符(d -> l -> r -> o -> w)。

最后,在第5行,使用toString()方法返回StringBuilder。

因此,字符串"world"被反转。

英文:

The method can be broken down into 5 key areas:

public static String reverse(String test) {
        return IntStream.range(0, test.length())
                .map(i -> test.charAt(test.length() - i - 1))
                .collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append)
                .toString();
    }

On line 1, the String is passed into the method to be returned in the reverse order. For example, String test can be equal to "world"

On line 2, a stream of numbers are passed into the stream which are as long as the String that is passed in. If test was equal to "world", the stream of numbers would be 0,1,2,3,4

On line 3, the numbers are mapped into the number from the back of the String which they are present. So number 0 is mapped into the character that is test.length (5) -i (0) -1. So number 0 is mapped into character at point 4. This is equal to "d". This is done for all the other numbers so 0 = d, 1 = l, 2 = r, 3 = o, 4 = w.

On line 4, a new Stringbuilder is created and each character is appended into the StringBuilder so it will add the characters as they come (d -> l -> r -> o -> w)

Lastly on line 5, the StringBuilder is returned using the toString() method.

As a result, the String "world" is reversed.

huangapple
  • 本文由 发表于 2020年8月17日 22:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63452488.html
匿名

发表评论

匿名网友

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

确定