This is my forEach loop ,now I want to take the ouput for each row and return the same in an array any sort of help is appreciated . as of now I have tried something like this , like suppose I am expecting 2object from the response , but I am recieving only one object which is the second object or last object ,
public Validation save(Multipart file){
ArrayList<Item> dta = new ArrayList<>();
IntStream.range(0, data.size()).forEach(rowNo-> {
try {
Item item saveData(rowNo 2,data.get(rowNo),vlidation);
ret.add(item);
});
return dta;
}
}
CodePudding user response:
As some comments suggested you could solve this with streams, since you're already using them.
Simply map it and then call collect(Collectors.toList())
to get a list.
IntStream.range(0, data.size()).map(rowNo-> {
try {
return saveData(rowNo 2,data.get(rowNo),vlidation);
} catch(Exception ex) {
..
}
}).collect(Collectors.toList());
If you do not want to use streams you could loop and add the current item to a list:
ArrayList<Item> ret = new ArrayList<>;
IntStream.range(0, data.size()).forEach(rowNo-> {
try {
Item item = saveData(rowNo 2,data.get(rowNo),vlidation);
ret.add(item);
} catch(Exception ex) {
...
}
});
// ret is the list that you wanted