I want to have failfast behavior for any unsuccessful response and if all go successful then I return the last successful response as shown in following code.
for(int i=0;i<input.size(); i++){
Data data = service.getData(input.get(i));
if(data.isSuccessful() && i==input.size()-1){
return data;
}else if(!data.isSuccessful()){
return data;
}else{
return null;
}
}
I tried to replace above mention code with streams but not been able to do so far.Main issue is that I am not able to imitate i(index) variable behavior in java8 stream code.
resp = input.stream().map((input)->{service.getData()}).filter(
(resp)->{
if(!resp.isSuccessful())
return true;
else if(resp.isSuccessful() && last resp)//if somehow I figure out last element
return true;
else
return false;}).findFirst();