如何解决IntStream.iterate 与要添加的交替值
我想创建一个整数序列,最好是一个 IntStream
,它遵循某个条件。一些简单的例子来解释我想要做什么:
从 0 开始的前 10 个偶数的序列(这里我没有问题,我用下面的代码片段来做)
IntStream.iterate(0,i -> i + 2).limit(10);
//0,2,4,6,8,10,12,14,16,18
从 0 开始的前 10 个数字的序列,将 2 和 3 交替添加到原始数字;所需的输出:
//0,5,7,15,17,20,22
我也很想在这种情况下使用 IntStream.iterate()
或 IntStream.generate()
,但我自己无法实现。我正在使用经典的 for 循环,它可以工作,但对于这样一个相对简单的任务来说有点长
List<Integer> list = new ArrayList<>();
list.add(0);
for(int i = 1,j = 1; i < 10; i++,j *= -1){
if(j > 0){
list.add(list.get(i-1) + 2);
}
else{
list.add(list.get(i-1) + 3);
}
}
//and then stream over the list to get IntStream
list.stream().mapToInt(Integer::intValue);
使用 IntStream.iterate()
或 IntStream.generate()
实现与上述相同的任何简单方法吗?
对于三个或更多交替值,我不知道如何优雅地做到这一点。例如,如果我想通过将 +2、+5 和 +7 交替添加到具有所需输出的前一个数字来创建从 0 开始的前 10 个数字的序列:
//0,21,28,30,35,42
我曾考虑在具有 3 个 if-else 块或 switch case 的 for 循环中使用 i%3
但是当我需要更多交替值并且我必须添加更多 ifs
或 {{ 1}}。任何想法如何做到这一点?如果您认为 cases
或 IntStream.iterate()
不是解决所描述任务的正确方法,我也愿意接受其他方法。
解决方法
通过交替添加 2 或 3 来生成流,可以观察到每个 other 值将是 5 的倍数。因此,如果前一个值可以被 5 整除而没有余数,我们应该加2,否则我们应该加3。
.ps-widget__content {
@extend %list-reset;
ul {
border: 1px solid #d1d1d1;
li {
border-bottom: 1px solid #d1d1d1;
a {
display: block;
padding: 15px 20px;
line-height: 20px;
font-size: 16px;
font-weight: 500;
color: $color-heading;
text-transform: capitalize;
i {
margin-right: 10px;
}
&:hover {
background-color: $color-1st;
color: #fff;
}
}
&:last-child {
border-bottom: none;
}
&.active {
background-color: $color-1st;
a {
color: #fff;
}
}
}
}
}
对于两个交替的整数,会有这种方法不可行的情况。如果其中一个数字是另一个数字的因数,那将是不可能的。例如 2 和 4。
对于此类情况,您可以使用更通用的方法并在迭代器之外维护一个布尔值。
它不遵循函数式风格,因为您的函数有副作用,但是嘿,Java 不是函数式语言。它足够直观。
IntStream.iterate(0,i -> (i % 5 == 0) ? (i + 2) : (i + 3)).limit(10)
对于 3 个交替值,在一般情况下,您可以对布尔值执行类似的操作,但使用一个在 0、1 和 2 之间循环的整数计数器。
AtomicBoolean isTwo = new AtomicBoolean(true);
IntStream.iterate(0,i -> isTwo.getAndSet(!isTwo.get()) ? (i + 2) : (i + 4))
,
您可以使用 AtomicInteger
来实现此目的并保留要以交替方式添加的数字列表。
List<Integer> adds = List.of(2,3,7);
AtomicInteger x = new AtomicInteger(0);
int limit = 10;
return IntStream.iterate(1,i -> i + adds.get(x.getAndIncrement() % adds.size()))
.limit(limit)
.boxed()
.collect(Collectors.toList());
,
你可以从数学上看这个。假设您要交替添加 x1、x2、x3 ... xn。
前n项可以写成:
(0 * sum of all x's)
(0 * sum of all x's + x1)
(0 * sum of all x's + x1 + x2)
(0 * sum of all x's + x1 + x2 + x3)
...
(0 * sum of all x's + sum of all x's - xn)
接下来的 n 项可以用同样的方式书写,只是你用 1 替换了所有的 0。
(1 * sum of all x's)
(1 * sum of all x's + x1)
(1 * sum of all x's + x1 + x2)
(1 * sum of all x's + x1 + x2 + x3)
...
(1 * sum of all x's + sum of all x's - xn)
还有 next n 项,您将所有 1 替换为 2,依此类推。
所以我们只需要生成流 (1,2,4...) 和 flatMap
每个元素 i
到流
(i * sum of all x's)
(i * sum of all x's + x1)
(i * sum of all x's + x1 + x2)
(i * sum of all x's + x1 + x2 + x3)
...
(i * sum of all x's + sum of all x's - xn)
使用此模式,您可以像这样编写 2、5、7 流:
final int a = 2,final int b = 5,final int c = 7;
final int sum = a + b + c;
IntStream.iterate(0,i -> i + 1).flatMap(
i -> IntStream.of(i * sum,i * sum + a,i * sum + a + b)
).limit(10);
如果您想改为执行 2、3、5、7:
final int a = 2,final int b = 3,final int c = 5;
final int d = 7;
final int sum = a + b + c + d;
IntStream.iterate(0,i * sum + a + b,i * sum + a + b + c)
).limit(10);
我会让您将其推广到任何 int[]
数字。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。