如何应用操作每第 n 个元素
How to apply operation every nth element
给定一个数字字符串,我想对字符串中的每第 n 个数字应用一个运算。
java 流是否可行?
例如,对于字符串 "12345"
和对每个第 2 个字符应用 3 的总和,结果将是 "15375"
.
public static void main(String[] args) {
"12345".chars()
.mapToObj(Character::getNumericValue)
.mapToInt(c -> c + 3) //should only happen every 2nd character
.forEach(System.out::print);
}
以上结果为 45678
,因为总和应用于所有字符。
我相信这就是您要找的:
public static void main(String[] args) {
String string = "12345";
String result = IntStream.range(0, string.length())
.mapToObj(index -> incrementCharAtEvenIndex(string, index, 3))
.collect(Collectors.joining());
System.out.println(result);
}
static String incrementCharAtEvenIndex(String string, int index, int increment) {
int number = Character.getNumericValue(string.charAt(index));
if ((index + 1) % 2 == 0) {
number = number + increment;
}
return String.valueOf(number);
}
不幸的是,Stream API 不是为直接使用索引而构建的,因此您必须使用 IntStream 来模拟 for-loop 和自定义方法来增加出现在偶数索引(记住它从 0 开始)。
您可以使用范围运算符将索引添加到流中:
String myString = "12345";
int n = 2;
IntStream
.range(0, myString.length())
.mapToObj(i -> Map.entry(i, Character.getNumericValue(myString.charAt(i))))
.mapToInt(pair -> (pair.getKey() + 1) % n == 0 ? pair.getValue() + 3 : pair.getValue())
.forEach(System.out::print);
你可以这样做。
String s = "12345";
String result = update(s, 2, 3);
System.out.println(result);
打印
15375
- 接受一个字符串,跳过值,然后递增
- 使用
(i+1)%n
以 nth
个字符开头。
- 使用三元运算符(
?:
)根据索引的余数映射到跳值的和
- return 新字符串
public static String update(String str, int n, int inc) {
return IntStream.range(0, str.length())
.mapToObj(i -> String.valueOf((i + 1) % n == 0 ?
str.charAt(i) - '0' + inc :
str.charAt(i) - '0'))
.collect(Collectors.joining());
}
我实际上有一个使用索引的解决方案,但我不确定是否有办法避免这种情况。显然不是。
这也有效:
public static void main(String[] args) {
String input = "12345";
String collect = IntStream.range(0, input.length())
.map(index -> {
int value = getNumericValue(input.charAt(index));
return (index % 2 != 0) ? value + 3 : value;
})
.mapToObj(String::valueOf)
.collect(Collectors.joining());
System.out.println(collect);
}
给定一个数字字符串,我想对字符串中的每第 n 个数字应用一个运算。 java 流是否可行?
例如,对于字符串 "12345"
和对每个第 2 个字符应用 3 的总和,结果将是 "15375"
.
public static void main(String[] args) {
"12345".chars()
.mapToObj(Character::getNumericValue)
.mapToInt(c -> c + 3) //should only happen every 2nd character
.forEach(System.out::print);
}
以上结果为 45678
,因为总和应用于所有字符。
我相信这就是您要找的:
public static void main(String[] args) {
String string = "12345";
String result = IntStream.range(0, string.length())
.mapToObj(index -> incrementCharAtEvenIndex(string, index, 3))
.collect(Collectors.joining());
System.out.println(result);
}
static String incrementCharAtEvenIndex(String string, int index, int increment) {
int number = Character.getNumericValue(string.charAt(index));
if ((index + 1) % 2 == 0) {
number = number + increment;
}
return String.valueOf(number);
}
不幸的是,Stream API 不是为直接使用索引而构建的,因此您必须使用 IntStream 来模拟 for-loop 和自定义方法来增加出现在偶数索引(记住它从 0 开始)。
您可以使用范围运算符将索引添加到流中:
String myString = "12345";
int n = 2;
IntStream
.range(0, myString.length())
.mapToObj(i -> Map.entry(i, Character.getNumericValue(myString.charAt(i))))
.mapToInt(pair -> (pair.getKey() + 1) % n == 0 ? pair.getValue() + 3 : pair.getValue())
.forEach(System.out::print);
你可以这样做。
String s = "12345";
String result = update(s, 2, 3);
System.out.println(result);
打印
15375
- 接受一个字符串,跳过值,然后递增
- 使用
(i+1)%n
以nth
个字符开头。 - 使用三元运算符(
?:
)根据索引的余数映射到跳值的和 - return 新字符串
public static String update(String str, int n, int inc) {
return IntStream.range(0, str.length())
.mapToObj(i -> String.valueOf((i + 1) % n == 0 ?
str.charAt(i) - '0' + inc :
str.charAt(i) - '0'))
.collect(Collectors.joining());
}
我实际上有一个使用索引的解决方案,但我不确定是否有办法避免这种情况。显然不是。
这也有效:
public static void main(String[] args) {
String input = "12345";
String collect = IntStream.range(0, input.length())
.map(index -> {
int value = getNumericValue(input.charAt(index));
return (index % 2 != 0) ? value + 3 : value;
})
.mapToObj(String::valueOf)
.collect(Collectors.joining());
System.out.println(collect);
}