My application structure is as follows: A controller that calls a service class that in turn calls a repository class. The repository(objectRepo) returns a list of Objects. The service class parses thru this list of objects it gets from repository and transforms it into an apiResponseWrapper class. The controller gives this apiResponseWrapper to user without changing it.
In my Service class, I have code that effectively translates to below sample code:
List<Object> objectList = objectRepo.getList(paramMap);
List<Entity> entityList = new ArrayList<>();
objectList.parallelStream().forEach(object->{
//code to transform object
entityList.add(transformedObject);
});
return entityList;
When parallelstream is used like above, the size of the response of my API varies on consecutive calls, with the exact same parameters, and on further inspection certain entities are absent in responses of few of those calls. On removal of parallelStream the size of response remains same, and all expected entities are in the result.
I would like to know if there is any specific way/any set of guidelines to be followed that would help me to use parallelstream without any data being dropped.