想要在下面的代码段中使用Streams API并删除当前for循环:
public List<MyObject> performSomeAction(){
List<String> myList= Arrays.asList("abc","xyz","def");
List<MyObject> resultList=new ArrayList<MyObject>();
int startIndex=0;
int totalNumOfRecords=1000;
for(int i=0;i<totalNumOfRecords;i++){
SomeObject o= xService.doSomething();
String type = o.getType();
int length = o.getLength();
if(myList.contains(type)){
int[] myarray=getMyArray(startIndex+20, startIndex+length);
String id=o.getSomeId();
OtherObject obj= xService.performOperation(myarray,"abcd");
resultList.add(new MyObject(obj,id));
}
startIndex+=length;
}
return resultList;
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
IntStream.range(0,totalNumOfRecords).forEach(i -> xService.doSomething());
Run Code Online (Sandbox Code Playgroud)
但我陷入困境,没有得到进一步的进展,需要从派生对象'o'中取出多个值,并使用它在它上面执行过滤器再次使另一个方法调用得到一些数组,将其传递给其他方法返回我需要创建resultList的其他对象