繁体   English   中英

我如何将此for循环转换为Java 8流模拟

[英]How can I convert this for loop to java 8 stream analog

如何将这段代码转换为Java 8流。

试图为每个使用,但失败了。 For循环代码正常工作。

for(RestrictedInstrument restrictedInstrument : restrictedInstruments){
    List<Identifier> identifierList = restrictedInstrument.getIdentifier();
    setTicker(matrix, identifierList);
}

应该使用矩阵Object和identifierList调用setTicker()方法。

您可以只使用List.forEach()

restrictedInstruments.forEach(i -> setTicker(matrix, i.getIdentifier()));

假设limitedInstruments是列表,首先映射到identifierList,然后使用Stream.forEach()执行setTicker(...)方法

restrictedInstruments
  .stream()
  .map( RestrictedInstrument::getIdentifier )
  .forEach( identifierList -> setTicker(matrix, identifierList) )

对于数组,只需使用Arrays.stream(limitedInstruments)

您可以流式传输列表,然后简单地传递仅调用setTicker函数的使用者。

 restrictedInstruments.stream()
.forEach(identifierList -> setTicker(matrix, identifierList.getIdentifier()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM