简介
Java 按照指定间隔生成连续递增的List 列表(引入Stream 类和流操作来提高效率):
1. 生成递增的List< Integer>
@Test
public void test009(){
int start = 1;
int interval = 2;
int count = 10;
List<Integer> list = IntStream.iterate(start, n -> n + interval)
.limit(count)
.boxed()
.collect(Collectors.toList());
System.out.println(list);
}
2. 生成递增的List< Double>
@Test
public void test008(){
double start = 0.0; // 起始值
double interval = 0.5; // 间隔
int count = 10; // 生成数量
List<Double> list = DoubleStream.iterate(start, n -> n + interval)
.limit(count)
.boxed()
.collect(Collectors.toList());
System.out.println(list);
}
总结
啊吧啊吧…