Java 如何将顺序元素添加到具有流的列表中
请注意:Java 如何将顺序元素添加到具有流的列表中,java,list,java-stream,Java,List,Java Stream,请注意: List<Integer> list = new LinkedList<>(); for (int i = 0; i < upperBound; i += step) { list.add(i); } List List=newlinkedlist(); for(int i=0;ii*步骤) .boxed() .collect(toCollection(LinkedList::new)); 请注意,定义上限并不
List<Integer> list = new LinkedList<>();
for (int i = 0; i < upperBound; i += step) {
list.add(i);
}
List List=newlinkedlist();
for(int i=0;i
如何用函数样式的流替换它
谢谢您的循环看起来很好
如果您确实想要使用流,您可以创建一个IntStream
,并将其放入列表中。例如:
int elementCount = upperBound / step;
if (upperBound % step != 0) elementCount++;
List<Integer> list = IntStream.range(0, elementCount)
.map(i -> i * step)
.boxed()
.collect(toCollection(LinkedList::new));
int elementCount=上限/步长;
如果(上限%step!=0)elementCount++;
List List=IntStream.range(0,elementCount)
.map(i->i*步骤)
.boxed()
.collect(toCollection(LinkedList::new));
请注意,定义上限并不简单。您的循环看起来不错
如果您确实想要使用流,您可以创建一个IntStream
,并将其放入列表中。例如:
int elementCount = upperBound / step;
if (upperBound % step != 0) elementCount++;
List<Integer> list = IntStream.range(0, elementCount)
.map(i -> i * step)
.boxed()
.collect(toCollection(LinkedList::new));
int elementCount=上限/步长;
如果(上限%step!=0)elementCount++;
List List=IntStream.range(0,elementCount)
.map(i->i*步骤)
.boxed()
.collect(toCollection(LinkedList::new));
请注意,定义上限并不简单。您可以使用IntStream
中的range
函数:
List<Integer> collect = IntStream.range(startRange, upperBound).filter(x -> (x % step) == 0).boxed().collect(toCollection(LinkedList::new));
List collect=IntStream.range(startRange,上限).filter(x->(x%step)==0.boxed().collect(toCollection(LinkedList::new));
您可以在此中找到更多信息。您可以使用IntStream
中的range
功能:
List<Integer> collect = IntStream.range(startRange, upperBound).filter(x -> (x % step) == 0).boxed().collect(toCollection(LinkedList::new));
List collect=IntStream.range(startRange,上限).filter(x->(x%step)==0.boxed().collect(toCollection(LinkedList::new));
您可以在此中找到更多信息,您为什么要这样做?背后有逻辑吗?你为什么要这么做?背后有逻辑吗?