使用流和减少将 Java 字符串中的第 n 个字符乘以 2?
Multiplying nth character in Java String by 2 using stream and reduce?
尝试使用 Java 流并减少这段代码
//get string as char array
char[] chars = ccNumber.toCharArray();
// build array list to store integer value of each char
List<Integer> multipliedChars = new ArrayList<>();
//get char values on even positions of the array, multiply them by two and add them to array list
for (int i = 1; i < chars.length; i+=2) {
multipliedChars.add((chars[i] * 2) / 10);
}
Java 8版本
return IntStream.range(0, ccNumber.length())
.filter(n -> n % 2 != 0)
.map(ccNumber::charAt)
.reduce(1 , (x) -> (x * 2) / 10)
.sum();
但是它抱怨说我在 reduce 方法中的参数与我正在尝试做的不兼容?
有什么解决办法吗?
本来我在想这里需要用第二种map
方法。但实际上我们仍然可以使用 reduce
:
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.reduce(0, (result,next) -> result + next / 5));
根据 javadocs reduction
reduce(
0,
(result, next) -> result + next / 5)
The reduce
operation takes two arguments:
identity
: The identity element is both the initial value of the reduction and the default result if there are no elements in the
stream. In this example, the identity element is 0; this is the
initial value of the sum and the default value if no members
exist in the collection.
accumulator
: The accumulator function takes two parameters: a partial result of the reduction (in this example, the sum of all
processed integers so far) and the next element of the stream (in this
example, an integer). It returns a new partial result. In this
example, the accumulator function is a lambda expression that adds two
Integer values and returns an Integer value:
(result, next) -> result + next * / 5)
现在我们可以使用第二个 map
而不是 reduce
来做同样的事情:
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.map(x -> x / 5)
.sum();
结果将相同:
assert IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.map(x -> x / 5)
.sum()
==
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.reduce(0, (result, next) -> result + next / 5);
但是如果需要采集值,我们需要使用map
:
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.map(x -> x / 5).boxed()
.collect(Collectors.toList());
正如@Holger 在下面的评论中指出的那样“除以十仍然没有任何意义。但是无论如何,乘以二,然后除以十,与除以五相同”
尝试使用 Java 流并减少这段代码
//get string as char array
char[] chars = ccNumber.toCharArray();
// build array list to store integer value of each char
List<Integer> multipliedChars = new ArrayList<>();
//get char values on even positions of the array, multiply them by two and add them to array list
for (int i = 1; i < chars.length; i+=2) {
multipliedChars.add((chars[i] * 2) / 10);
}
Java 8版本
return IntStream.range(0, ccNumber.length())
.filter(n -> n % 2 != 0)
.map(ccNumber::charAt)
.reduce(1 , (x) -> (x * 2) / 10)
.sum();
但是它抱怨说我在 reduce 方法中的参数与我正在尝试做的不兼容?
有什么解决办法吗?
本来我在想这里需要用第二种map
方法。但实际上我们仍然可以使用 reduce
:
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.reduce(0, (result,next) -> result + next / 5));
根据 javadocs reduction
reduce(
0,
(result, next) -> result + next / 5)
The
reduce
operation takes two arguments:
identity
: The identity element is both the initial value of the reduction and the default result if there are no elements in the stream. In this example, the identity element is 0; this is the initial value of the sum and the default value if no members exist in the collection.
accumulator
: The accumulator function takes two parameters: a partial result of the reduction (in this example, the sum of all processed integers so far) and the next element of the stream (in this example, an integer). It returns a new partial result. In this example, the accumulator function is a lambda expression that adds two Integer values and returns an Integer value:(result, next) -> result + next * / 5)
现在我们可以使用第二个 map
而不是 reduce
来做同样的事情:
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.map(x -> x / 5)
.sum();
结果将相同:
assert IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.map(x -> x / 5)
.sum()
==
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.reduce(0, (result, next) -> result + next / 5);
但是如果需要采集值,我们需要使用map
:
IntStream.range(0, str.length())
.filter(n -> n % 2 != 0)
.map(str::charAt)
.map(x -> x / 5).boxed()
.collect(Collectors.toList());
正如@Holger 在下面的评论中指出的那样“除以十仍然没有任何意义。但是无论如何,乘以二,然后除以十,与除以五相同”